The sleep function when written at the end, forces the program to sleep,
preventing him to execute any printf() instructions before it.
Attaching a program.
-- 
Vikas Bansal
Student(ECE undergraduate final year)
The L.N.Mittal Institute of Information Technology,Rajasthan,India.
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHMSIZE 27

main()
{
printf("going to sleep-start");
char c;
int shmid;
key_t key;
char *shm, *s;
printf("going to sleep-start");
/* * We'll name our shared memory segment * "5678". */

key = 5678;

/* * create the segment.* */
if ((shmid = shmget((key_t)key, SHMSIZE, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
printf("error");
exit(1);
}

/** Now we attach the segment to our data space.*/
if ((shm = shmat(shmid,0, 0)) == (char *) -1)
{
perror("shmat");
printf("error");
exit(1);
}
printf("going to sleep-mid");

/** Now put some things into the memory for the other process to read. */
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';

/** Finally, we wait until the other process
* Changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/

printf("going to sleep");
while (*shm != '*')
sleep(1);
exit(0);

}

Reply via email to