fork download
  1. /*
  2. * shared memory - header file
  3. *
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <errno.h>
  11. #include <semaphore.h>
  12.  
  13. #define SHMKEY 0x123
  14. #define PERMS 0666
  15.  
  16. extern int errno;
  17.  
  18. typedef struct
  19. {
  20. sem_t sem1;
  21. sem_t sem2;
  22. }CFG_SHM;
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <pthread.h>
  27. #include <semaphore.h>
  28. #include <unistd.h>
  29.  
  30.  
  31. int main(void)
  32. {
  33. int childpid;
  34. int val;
  35. CFG_SHM *cfgshmPtr,cfgdata;
  36. int shmid;
  37.  
  38. shmid = shmget(SHMKEY,sizeof(CFG_SHM),IPC_CREAT|PERMS); //create the shared
  39. if(shmid < 0) //memory segment
  40. {
  41. printf("Error in getting shared memory, %s",strerror(errno));
  42. exit(0);
  43. }
  44. cfgshmPtr = (CFG_SHM *) shmat(shmid,0,0);
  45. if(cfgshmPtr == (CFG_SHM *) 0)
  46. {
  47. printf("Error in attaching shared memory, %s",strerror(errno));
  48. exit(0);
  49. }
  50. memcpy((void *)&cfgdata,(void *)cfgshmPtr,sizeof(CFG_SHM));
  51.  
  52.  
  53. sem_init(&(cfgshmPtr->sem1), 0, 1);
  54. sem_init(&(cfgshmPtr->sem2), 0, 0);
  55. childpid=fork();
  56. if(childpid < 0)
  57. {
  58. printf("Fork failed\n");
  59. exit(1);
  60. }
  61. if (childpid==0)
  62. {
  63. int OddNum=1;
  64. printf("The number is 1 Odd \n");
  65. while (OddNum < 9)
  66. {
  67. shmid = shmget(SHMKEY,sizeof(CFG_SHM),IPC_CREAT|PERMS); //create the shared
  68. if(shmid < 0) //memory segment
  69. {
  70. printf("Error in getting shared memory, %s",strerror(errno));
  71. exit(0);
  72. }
  73. cfgshmPtr = (CFG_SHM *) shmat(shmid,0,0); //attach it to the
  74. if(cfgshmPtr == (CFG_SHM *) 0) //process
  75. {
  76. printf("Error in attaching shared memory, %s",strerror(errno));
  77. exit(0);
  78. }
  79. /*
  80. sem_getvalue(&(cfgshmPtr->sem1), &val);
  81. printf("Child :The sem1 value is %d \n", val);
  82. sem_getvalue(&(cfgshmPtr->sem2), &val);
  83. printf("Child :The sem2 value is %d \n", val);
  84. */
  85. sem_wait(&(cfgshmPtr->sem2));
  86. OddNum=OddNum+2;
  87. printf("The number is %d Odd \n", OddNum);
  88. sem_post(&(cfgshmPtr->sem1));
  89. if(shmdt(cfgshmPtr) < 0) //detach the shared memory segment
  90. printf("Error in detaching shared memory segment, %s\n",strerror(errno));
  91. }
  92. }
  93. else
  94. {
  95. int EvenNum=0;
  96. while (EvenNum < 10)
  97. {
  98. shmid = shmget(SHMKEY,sizeof(CFG_SHM),IPC_CREAT|PERMS); //create the shared
  99. if(shmid < 0) //memory segment
  100. {
  101. printf("Error in getting shared memory, %s",strerror(errno));
  102. exit(0);
  103. }
  104. cfgshmPtr = (CFG_SHM *) shmat(shmid,0,0); //attach it to the
  105. if(cfgshmPtr == (CFG_SHM *) 0) //process
  106. {
  107. printf("Error in attaching shared memory, %s",strerror(errno));
  108. exit(0);
  109. }
  110. /*
  111. sem_getvalue(&(cfgshmPtr->sem1), &val);
  112. printf("Parent :The sem1 value is %d \n", val);
  113. sem_getvalue(&(cfgshmPtr->sem2), &val);
  114. printf("Parent :The sem2 value is %d \n", val);
  115. */
  116. sem_wait(&(cfgshmPtr->sem1));
  117. EvenNum=EvenNum+2;
  118. printf("The number is %d Even \n", EvenNum);
  119. sem_post(&(cfgshmPtr->sem2));
  120. if(shmdt(cfgshmPtr) < 0) //detach the shared memory segment
  121. printf("Error in detaching shared memory segment, %s\n",strerror(errno));
  122. }
  123. if(shmctl(shmid,IPC_RMID,0) < 0) //remove the shared memory segment
  124. printf("Error in removing shared memory segment, %s\n",strerror(errno));
  125. else
  126. printf("Shared memory removed successfully\n");
  127.  
  128. }
  129. }
  130.  
  131.  
  132.  
  133.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty