Exp5 PDF
Exp5 PDF
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
struct shmseg {
int cntr;
int write_complete;
int read_complete;
};
int total_count;
int cntr;
int sleep_time;
if (argc != 2)
total_count = 10000;
else {
total_count = atoi(argv[1]);
total_count = 10000;
if (shmid == -1) {
perror("Shared memory");
return 1;
return 1;
while (shmp->write_complete != 1) {
if (shmp->cntr == -1) {
perror("read");
return 1;
sleep(3);
shmp->read_complete = 1;
if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
return 0;
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#define MAX_TRIES 20
struct shmseg {
int cntr;
int write_complete;
int read_complete;
};
void remove_semaphore();
int shmid;
char *bufptr;
int total_count;
int sleep_time;
pid_t pid;
if (argc != 2)
total_count = 10000;
else {
total_count = atoi(argv[1]);
total_count = 10000;
if (shmid == -1) {
perror("Shared memory");
return 1;
return 1;
shmp->cntr = 0;
pid = fork();
if (pid > 0) {
} else if (pid == 0) {
return 0;
} else {
perror("Fork Failure\n");
return 1;
while (shmp->read_complete != 1)
sleep(1);
if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
perror("shmctl");
return 1;
remove_semaphore();
return 0;
int cntr;
int numtimes;
int sleep_time;
int semid;
int tries;
int retval;
if (semid >= 0) {
printf("First Process\n");
sem_buf.sem_op = 1;
sem_buf.sem_flg = 0;
sem_buf.sem_num = 0;
if (retval == -1) {
return;
int ready = 0;
printf("Second Process\n");
if (semid < 0) {
return;
sem_buf.sem_num = 0;
sem_buf.sem_op = 0;
sem_buf.sem_flg = SEM_UNDO;
if (retval == -1) {
return;
sem_buf.sem_num = 0;
sem_buf.sem_flg = SEM_UNDO;
if (retval == -1) {
return;
cntr = shmp->cntr;
shmp->write_complete = 0;
if (pid == 0)
cntr += 1;
shmp->cntr = cntr;
if (sleep_time == 0)
sleep(1);
shmp->write_complete = 1;
if (retval == -1) {
perror("Semaphore Locked\n");
return;
if (pid == 0)
return;
void remove_semaphore() {
int semid;
int retval;
if (semid < 0) {
return;
if (retval == -1) {
return;
return;