garimella
Full Member level 5
I am trying to write a simple device driver and application program to access the driver.
Driver write system call looks like this
and application program which works looks as follows
Now if I add one more sprintf function as shown above , it leads to segmentation fault.I do not know why?. If I remove this line, the code works fine. What could be the problem? The same problem happens when I call sprintf and write as a subfunction call?
Driver write system call looks like this
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) { short c=0; short ind =0; short count=0; memset(msg,0,100); readPos=0; while(len>0) { msg[count++]=buff[ind++]; len--; }
and application program which works looks as follows
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int main() { char buf[100]; char i = 0; char tag=0; int fp; char *val[10]; char *rval[10]; unsigned int a =18440; unsigned int d =1; memset(buf, 0, 10); tag=1; fp = open("/dev/dd",O_RDWR); if (fp<0) { printf("file failed to open\n"); } sprintf(val[1],"%d%x%x",tag,a,d); write(fp,val[1],strlen(val[1])); sprintf(rval[1],"%d%x",tag,a);// statement that causes segmentation fault return 0; }
Now if I add one more sprintf function as shown above , it leads to segmentation fault.I do not know why?. If I remove this line, the code works fine. What could be the problem? The same problem happens when I call sprintf and write as a subfunction call?
Last edited by a moderator: