void barber() { printf("barber started\n"); while(1) { sem_change(sem_ID, 0, -1); if(customers_count==0) { printf("barber sleeping\n"); eflag=1; sem_change(sem_ID, 0, 1); sem_change(sem_ID, 1, -1); sem_change(sem_ID, 0, -1); } customers_count--; sem_change(sem_ID, 0, 1); sem_change(sem_ID, 3, 1); // printf("tail:%d\n", tail); // pthread_mutex_lock(&B); sem_change(sem_ID, 4, -1); // pthread_mutex_unlock(&B); } }
void consumer() { printf("consumer started\n"); while(1) { int v=0; sem_change(sem_ID, 0, -1); if(count==0) { printf("Consumer Waiting\n"); eflag=1; sem_change(sem_ID, 0, 1); sem_change(sem_ID, 1, -1); sem_change(sem_ID, 0, -1); //Waits until an item is produced } count--; printf("consumed item:%d \n", buffer[tail]); tail=(tail+1)%MAX_BUF; if(count==MAX_BUF-1 && fflag==1) { fflag=0; sem_change(sem_ID, 2, 1); //To signal the producer thread that } buffer is not full sem_change(sem_ID, 0, 1); sleep(rand()%4); } }
void producer() { printf("Producer started\n"); while(1) { int v=0; sem_change(sem_ID, 0, -1); if(count==MAX_BUF) { printf("Producer waiting\n"); fflag=1; sem_change(sem_ID, 0, 1); sem_change(sem_ID, 2, -1); sem_change(sem_ID, 0, -1); //Waits until a free slot is available } count++; buffer[head]=item_id++; if(count==1 && eflag==1) { eflag=0; sem_change(sem_ID, 1, 1); //To signal consumer thread that buffer is not empty } printf("produced item:%d \n", buffer[head]); head=(head+1)%MAX_BUF; sem_change(sem_ID, 0, 1); sleep(rand()%3); } }
void write_thread() { char arg1[10], arg2[10000], ch; while(1) { scanf("%s", arg1); scanf("%c", &ch); // consumes the space in between fgets(arg2, sizeof(arg2), stdin); //printf("%s\n%s", arg1, arg2); printf("\n1..2 -> %s %s\n", arg1, arg2); sem_change(write_sem_id, 0, -1); if(isdigit(arg1[0])) { if(atoi(arg1) > *(pids)) // invalid reference ID { printf("Invalid reference ID\n"); } else if(ref_id == atoi(arg1))//sender and receiver are the same, { printf("Self-addressing is not viable\n"); } else { m->send_index = ref_id; m->recv_index = atoi(arg1); strcpy(m->msg, arg2); } } else if(arg1[0] == ':' && arg1[1] == 'a' && arg1[2] == 'l' && arg1[3] == 'l') // ":all" { m->recv_index = -1; strcpy(m->msg, arg2); } else { printf("Please enter in the correct format\n"); } sem_change(write_sem_id, 1, 1); } }
void customer(void *arg) { //printf("C\n"); sem_change(sem_ID, 0, -1); if(customers_count==MAX_C) // If all seats are occupied exit the thread { int *ptr=(int*)arg; *ptr=0; printf("No place for customer %d so leaving\n", pthread_self()); sem_change(sem_ID, 0, 1); return; } customers_count++; if(customers_count==1 && eflag==1) { sem_change(sem_ID, 1, 1); eflag=0; } sem_change(sem_ID, 0, 1); printf("Customer %d got a place\n", pthread_self()); sem_change(sem_ID, 3, -1); printf("Cutting for %d customer\n", pthread_self()); sleep(rand()%5+4); sem_change(sem_ID, 4, 1); // printf("head:%d\n", head); // pthread_mutex_lock(&B); // pthread_mutex_unlock(&B); int *ptr=(int*)arg; *ptr=0; }
int main() { printf("hell\n"); signal(SIGUSR1, sigusr1); char pathname[1000]; getwd(pathname); //creating the write-semaphore(two) key_t keyw = ftok(pathname, 'A'); write_sem_id = semget(keyw, 2, IPC_CREAT|0666); if(write_sem_id < 0) { printf("Semaphore init error\n"); exit(1); } printf("%d %d", sem_val(write_sem_id, 0), sem_val(write_sem_id, 1)); //acquire semaphore to register the process.. sem_change(write_sem_id, 0, -1); printf("%d %d", sem_val(write_sem_id, 0), sem_val(write_sem_id, 1)); //accessing shared memory for storing process IDs of max 50 processes //the first memory location stores the current number of active processes key_t pid_shm_key = ftok(pathname, 'C'); int pid_shm_id = shmget(pid_shm_key, 60*sizeof(int), IPC_CREAT|0666); if(pid_shm_id < 0) { printf("shmget error\n"); exit(1); } pids = (int*)shmat(pid_shm_id, NULL, 0); *(pids) = *(pids) + 1; //increase the number of active processes.. ref_id = *(pids); *(pids + ref_id) = getpid(); printf("Registered PID of this process: %d\nThe reference ID for this process is: %d\n", getpid(), ref_id); sem_change(write_sem_id, 0, 1); //creating shared memory for storing message contents //can store maximum of 10000 characters. //Each process/client(max 50) has its own memory-block //0th block is server's. Rest are allocated according to reference IDs.. key_t msg_shm_key = ftok(pathname, 'D'); int msg_shm_id = shmget(msg_shm_key, 60*sizeof(struct message), IPC_CREAT|0666); if(msg_shm_id < 0) { printf("shmget error\n"); exit(1); } m = (struct message *)shmat(msg_shm_id, NULL, 0); if(m == (struct message*)-1) { printf("shmat error\n"); exit(1); } //creating tot_read_count key_t count_shm_key = ftok(pathname, 'H'); int count_shm_id = shmget(count_shm_key, sizeof(int), IPC_CREAT|0666); if(count_shm_id < 0) { printf("shmget error\n"); exit(1); } count = (int*)shmat(count_shm_id, NULL, 0); if(count == (int*)-1) { printf("shmat error\n"); exit(1); } printf("%d %d %d %d %d %d\n", write_sem_id, pid_shm_id, msg_shm_id, count_shm_id); printf("%d %d", sem_val(write_sem_id, 0), sem_val(write_sem_id, 1)); sleep(5); pthread_create(&reader, NULL, (void *)&read_thread, NULL); pthread_create(&writer, NULL, (void *)&write_thread, NULL); pthread_join(reader, NULL); pthread_join(writer, NULL); return 0; }
int main(){ int prod_count=0; int con_count = 0; int shmid; key_t key=1234; int pid; int *shm; int retstatus; int sem_id; int *arr=(int *)malloc(3); sem_id=sem_init(3,arr); printf("Sem id: %d\n",sem_id); shm=(int *)malloc(50); if((shmid = shmget(key,27,IPC_CREAT|0666))<0) { printf("SHM error...........\n"); exit(0); } if((shm=(int *)shmat(shmid,NULL,0)) == (char *)-1){ printf("Attachment error\n"); exit(0); } pid=fork(); if(pid == 0) { printf("Producer is going on:\n"); while(count < max_count) { sem_change(sem_id,0,1); count++; sem_change(sem_id,1,1); *(shm+prod_count)=value; printf("Producer produced : %d\n",value); value++; prod_count++; if(count == 1) sem_change(sem_id,2,1); sem_change(sem_id,1,-1); sem_change(sem_id,0,-1); sleep(1); } } else{ int pid2=fork(); if(pid2 == 0){ printf("Consumer is starting"); while(con_count < max_count){ sem_change(sem_id,0,1); count--; sem_change(sem_id,2,1); printf("Value consumed: %d\n",*(shm+con_count)); con_count++; if(count < max_count) sem_change(sem_id,1,1); sem_change(sem_id,2,-1); sem_change(sem_id,0,-1); sleep(3); } } else{ pid=wait(retstatus); } } exit(0); }