Question: Use perror or strerror. Also Line error, 25: warning: cast from pointer to integer of different size Please help. code: #include #include #include #include #include
Use perror or strerror.
Also Line error, 25: warning: cast from pointer to integer of different size
Please help.
code:
#include
#include
#include
#include
#include
void main(int argc, char *argv[])
{
int shmid;
int *total;
pid_t pid;
int status;
int flag;
int n;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
if (shmid < 0)
{
printf("shmget error (server) ");
exit(1);
}
total = (int *) shmat(shmid, NULL, 0);
if ((int) total == -1)
{
printf("shmat error (server) ");
exit(1);
}
*total = 0;
printf("Parent:My pid is %d; now spawning a child after setting the shared memory to 0 ",getpid());
pid = fork();
if (pid < 0)
{
printf("*** fork error (server) *** ");
exit(1);
}
else if (pid == 0)
{
flag = 1;
printf("Child:My pid is %d; my parent's id is %d; the shared integer value is currently 0; I will spin untill it's not 0 ",getpid(),getppid());
while(flag == 1)
{
sleep(1);
//printf("I am alive ");
if (*total > 0 )
{
printf("Child:The value in the shared integer is now %d. I will set it back to 0 ",*total);
sleep(1);
*total = 0;
flag = 0;
}
}
printf("Child:Child process terminating ");
exit(0);
}
else
{
sleep(1);
printf("Parent:My pid is %d; spawned a child with pid %d. Please enter an integer to be stored in shared memory ",getpid(),pid);
scanf("%d",&n);
*total = n;
while( wait(NULL) > 0 )
{
if (*total == 0)
{
printf("Parent: the child has rezeroed our shared integer ");
}
sleep(1);
}
shmdt((void *) total);
printf("Parent:Child terminated.parent successfully removed segment whose id is %d ",shmid);
shmctl(shmid, IPC_RMID, NULL);
exit(0);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
