fork download
  1. #include<stdio.h>
  2. #include<sys/stat.h>
  3. #include<sys/types.h>
  4. #include<sys/shm.h>
  5. #include<sys/ipc.h>
  6. #include<sys/sem.h>
  7. #include<string.h>
  8. #define SIZE 10 int *integer=0;
  9. main()
  10. {
  11. int shmid;
  12. key-t key-10;
  13. char *shm;
  14. int semid,pid;
  15. shmid = shmget((key-1)10,SIZE,IPC_CREAT/0666);
  16. shm=shmat(shmid,NULL,0);
  17. semid = semget(0X20,1,IPC_CREAT/0666);
  18. integer = (int*)shm;
  19. pid = fork();
  20. if(pid==0)
  21. {
  22. int i = 0;
  23. while(i<10)
  24. {
  25. sleep(2);
  26. printf("\n child process uses shared memory");
  27. accessmem(semid);
  28. }
  29. }
  30. else
  31. {
  32. int j=0;
  33. while(j<10)
  34. {
  35. sleep(j);
  36. printf("\n parent versus shared memory");
  37. aceessmem(semid);
  38. j++;
  39. }
  40. }
  41. shmctl(semid,IPC_RMID,0);
  42. }
  43.  
  44. int accessmem(int semid)
  45. {
  46. struct sembuf sop;
  47. sop.sem_num=0;
  48. sop.sem_op=-1;
  49. sop.sem_flg=0;
  50. semop(semid,&sop,1);
  51. (*integer)++;
  52. printf("\t"integer varible=%d",(*integer));
  53. sop.sem_num=0;
  54. sop.sem_op=-1;
  55. sop.sem_flag=0;
  56. semop(semid,&sop,1);
  57. }
  58.  
  59.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/i386-linux-gnu/sys/shm.h:27:0,
                 from prog.c:4:
/usr/include/i386-linux-gnu/sys/ipc.h:24:3: warning: #warning "Files using this header must be compiled with _SVID_SOURCE or _XOPEN_SOURCE" [-Wcpp]
 # warning "Files using this header must be compiled with _SVID_SOURCE or _XOPEN_SOURCE"
   ^
prog.c:9:1: warning: return type defaults to 'int' [-Wimplicit-int]
 main()
 ^
prog.c: In function 'main':
prog.c:12:2: error: 'key' undeclared (first use in this function)
  key-t key-10;
  ^
prog.c:12:2: note: each undeclared identifier is reported only once for each function it appears in
prog.c:12:6: error: 't' undeclared (first use in this function)
  key-t key-10;
      ^
prog.c:12:8: error: expected ';' before 'key'
  key-t key-10;
        ^
prog.c:15:24: error: expected ')' before numeric constant
  shmid = shmget((key-1)10,SIZE,IPC_CREAT/0666);
                        ^
prog.c:15:10: error: too few arguments to function 'shmget'
  shmid = shmget((key-1)10,SIZE,IPC_CREAT/0666);
          ^
In file included from prog.c:4:0:
/usr/include/i386-linux-gnu/sys/shm.h:53:12: note: declared here
 extern int shmget (key_t __key, size_t __size, int __shmflg) __THROW;
            ^
prog.c:18:2: error: 'integer' undeclared (first use in this function)
  integer = (int*)shm;
  ^
prog.c:19:8: warning: implicit declaration of function 'fork' [-Wimplicit-function-declaration]
  pid = fork();
        ^
prog.c:25:4: warning: implicit declaration of function 'sleep' [-Wimplicit-function-declaration]
    sleep(2);
    ^
prog.c:27:4: warning: implicit declaration of function 'accessmem' [-Wimplicit-function-declaration]
    accessmem(semid);
    ^
prog.c:37:4: warning: implicit declaration of function 'aceessmem' [-Wimplicit-function-declaration]
    aceessmem(semid);
    ^
prog.c: In function 'accessmem':
prog.c:51:4: error: 'integer' undeclared (first use in this function)
  (*integer)++;
    ^
prog.c:52:13: error: expected ')' before 'integer'
  printf("\t"integer varible=%d",(*integer));
             ^
prog.c:52:31: warning: missing terminating " character
  printf("\t"integer varible=%d",(*integer));
                               ^
prog.c:52:13: error: missing terminating " character
  printf("\t"integer varible=%d",(*integer));
             ^
prog.c:57:1: error: expected ';' before '}' token
 }
 ^
prog.c:57:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty