PROGRAM 1
/* student name: anthony f. ortiz student id : xxx-xx-2871 login id : cs304-21 problems solved: all */ /* version: 1.0 */ /* copyright (c) edward billard, 1994 */ /* ---------------- demo.c -------------------- */ #include #include "pm.h" #include "demo.h" /* ex. 1 */ void big_demo() { int pid,i,sem,temp; char s[MAXLINE]; char *s1 = "** Hello **.^"; while(1) { pm_process(1,1); pm_resume(1); pm_putline("DEMO 1"); pm_putline("scheduling"); for (pid=2;pid<=8;pid++) { pm_process(pid,pid); pm_resume(pid); pm_suspend(pid); pm_resume(pid); } pm_putline("USE THE KEYBOARD."); pm_getline(s); pm_putline("priorities"); for (pid=1; pid<=8; pid++) pm_setprio(pid,1); pm_putline("semaphores"); sem = 0; for (pid=1;pid<=7;pid++) { pm_wait(sem); sem = (sem+1)%4; } sem = 0; for (pid=1;pid<=7;pid++) { pm_signal(sem); sem = (sem+1)%4; } pm_wait(0); pm_putline("messages"); for (pid=1;pid<=6;pid++) pm_receive(&temp); for (pid=6;pid>=1;pid--) pm_send(pid,0); /* pm_receive(&temp); */ pm_putline("sleeping"); for (pid=1;pid<=3;pid++) pm_sleep(pm_getpid()); pm_sleep(9999); pm_putline("characters"); for (i=0;i<=strlen(s1);i++) pm_putchar(s1[i]); pm_putline("destroy"); for (pid=1;pid<=8;pid++) pm_destroy(pid); } } /* ex. 2 */ void Control() { int temp; char s[MAXLINE]; while (1) { pm_putline("Start of Control Demo."); pm_putline("DO SIGNAL."); pm_wait(0); pm_putline("DO SEND."); pm_receive(&temp); pm_putline("DO WAKEUP."); pm_sleep(99999); pm_putline("DO KEYBOARD."); pm_getline(s); } } void init_control() { pm_create(1,1,Control,0); pm_resume(1); } /* ex. 3 */ void bad_tiny_demo() { while (1) { pm_process(1,1); pm_resume(1); pm_putline("THIS IS A TINY DEMO."); pm_suspend(1); pm_destroy(1); pm_process(2,1); pm_resume(2); pm_destroy(2); } } /* ex. 4 */ void bad_small_demo() { int pid, msg; while(1) { pm_process(1,1); pm_resume (1); pm_suspend (1); pm_resume (1); pm_process (2,2); pm_resume (2); pm_wait (2); pm_signal (2); msg = pm_receive (&pid); pm_send (2, 1 ); pm_destroy (2); pm_destroy (1); } } /* ex. 5 */ void infinite() { while (1) { /* infinite loop: no I/O */ pm_resched(FALSE); } } void outputter() { while (1) pm_putline("Running: process 2."); } #define SLEEP 15 void ok_timer() { while (1) pm_sleep(SLEEP); } #define LOWPRIORITY 1 #define HIGHPRIORITY 98 void bad_init_timeslice() { pm_create(1,LOWPRIORITY,infinite,0); pm_create(2,LOWPRIORITY,outputter,0); pm_create(3,LOWPRIORITY,ok_timer,0); pm_resume(1); pm_resume(2); pm_resume(3); } /* ex. 6 */ void bad_putcharacters() { char *s1 = "This is process: ^"; int i; while (1) { s1[16] = '0'+pm_getpid(); for (i=0;i<=strlen(s1);i++){ pm_wait(sem_putchar); pm_putchar(s1[i]); pm_signal(sem_putchar); } } } void bad_init_putcharacters() { sem_putchar= pm_seminit(1); pm_create(1,1,bad_putcharacters,0); pm_create(2,1,bad_putcharacters,0); pm_resume(1); pm_resume(2); } /* ex. 7 */ void bad_philosopher(id) int id; { int chop1,chop2; char s[MAXLINE]; if (id==1) { chop1 = 0; chop2 = 1; } if (id==2) { chop1 = 0; chop2 = 1; } while(1) { sprintf(s,"thinking philosopher: %d",id); pm_putline(s); pm_wait(chop1); sprintf(s,"philosopher: %d chopstick: %d",id,chop1); pm_putline(s); pm_wait(chop2); sprintf(s,"philosopher: %d chopstick: %d",id,chop2); pm_putline(s); sprintf(s,"eating philosopher : %d",id); pm_putline(s); pm_signal(chop1); pm_signal(chop2); } } void bad_init_philosopher() { pm_signal(0); /* chopsticks available */ pm_signal(1); pm_create(1,1,bad_philosopher,1); pm_create(2,1,bad_philosopher,2); pm_resume(1); pm_resume(2); } /* ex. 8 */ void bad_consumer() { while (1) { pm_wait(partially_full); pm_wait(sem_buffer); pm_putchar(buffer[out]); out = (out+1)%N; pm_signal(sem_buffer); pm_signal (partially_empty); } } void ok_producer() { while(1) { pm_wait(partially_empty); pm_wait(sem_buffer); buffer[in] = '0'+in; in = (in+1) % N; pm_signal(sem_buffer); pm_signal(partially_full); } } void bad_init_prodcons() { in = 0; out = 0; sem_buffer= pm_seminit(1); partially_empty = pm_seminit(N); partially_full = pm_seminit(0); pm_create(1,1,ok_producer,0); pm_create(2,1,bad_consumer,0); pm_resume(1); pm_resume(2); } /* ex. 9 */ void bad_writer() { char s[MAXLINE]; while (1) { pm_wait (sem_wrt); sprintf(s,"begin writing: %d",pm_getpid()); pm_putline(s); /* writer writes to the file. */ sprintf(s,"end writing: %d",pm_getpid()); pm_putline(s); pm_signal (sem_wrt); } } void ok_reader() { char s[MAXLINE]; while (1) { pm_wait(sem_readcount); readcount++; if (readcount==1) pm_wait(sem_wrt); pm_signal(sem_readcount); sprintf(s,"begin reading: %d",pm_getpid()); pm_putline(s); /* reader reads from the file. */ sprintf(s,"end reading: %d",pm_getpid()); pm_putline(s); pm_wait(sem_readcount); readcount--; if (readcount==0) pm_signal(sem_wrt); pm_signal(sem_readcount); sprintf(s,"other : %d",pm_getpid()); pm_putline(s); } } void bad_init_rdrwrt() { readcount=0; sem_readcount=pm_seminit(1); sem_wrt=pm_seminit(1); pm_create(1,1,ok_reader,0); pm_create(2,1,ok_reader,0); pm_create(3,1,bad_writer,0); pm_resume(1); pm_resume(2); pm_resume(3); } /* ex. 10 */ #define TOBACCO 1 #define PAPER 2 #define MATCHES 3 void bad_smoker(sem_need) int sem_need; { char s[MAXLINE]; while (1) { pm_wait(sem_need); sprintf(s,"smoker: %d ingredient: %d",pm_getpid(),sem_need); pm_putline(s); pm_signal (sem_need); } } void ok_agent() { int sem; while (1) { pm_signal(TOBACCO); pm_wait(TOBACCO); pm_signal(PAPER); pm_wait(PAPER); pm_signal(MATCHES); pm_wait(MATCHES); } } void bad_init_smoker() { pm_create(1,1,bad_smoker,TOBACCO); pm_resume(1); pm_create(2,1,bad_smoker,PAPER); pm_resume(2); pm_create(3,1,bad_smoker,MATCHES); pm_resume(3); pm_create(4,1,ok_agent,0); pm_resume(4); } /* ex. 11 */ void bad_deadlock2() { int temp; while(1) { pm_signal (0); pm_receive(&temp); } } void ok_deadlock1() { while(1) { pm_wait(0); pm_send(2,0); } } void bad_init_deadlock() { pm_create(1,1,ok_deadlock1,0); pm_create(2,1,bad_deadlock2,0); pm_resume(1); pm_resume(2); } /* ex. 12 */ #define SERVER_ID 1 #define FINISH_MSG 0 void bad_server() { int msg,client_id; char s[MAXLINE]; while(1) { msg = pm_receive(&client_id); /* server does the reqested action. */ sprintf(s,"server is serving client: %d message: %d",client_id,msg); pm_putline(s); pm_send (client_id, FINISH_MSG); } } void ok_client() { int pid=pm_getpid(); int msg=0; int serv; char s[MAXLINE]; while(1) { msg++; sprintf(s,"client: %d sending message: %d ",pid,msg); pm_putline(s); pm_send(SERVER_ID,msg); pm_receive(&serv); } } void bad_init_clientserver() { pm_create(1,1,bad_server,0); pm_create(2,1,ok_client,0); pm_create(3,1,ok_client,0); pm_resume(1); pm_resume(2); pm_resume(3); } /* ex. 13 */ #define RINGSIZE 8 void bad_ring() /* pid=1,2,3,4,5,6,7,8 */ { int msg,pid,nextpid; char s[MAXLINE]; while(1) { msg=pm_receive(&pid); msg++; pm_putchar('.'); nextpid = pm_getpid()+1; if (nextpid == 9) { nextpid = 1; } if (msg == RINGSIZE) { sprintf(s,"Message finished ring at: %d",pm_getpid()); pm_putline(s); } else if (msg < RINGSIZE) { pm_send (nextpid, msg); } } } void bad_init_ring() { int pid; for (pid=1; pid<=RINGSIZE; pid++) { pm_create(pid,1,bad_ring,0); pm_resume(pid); } } /* ex. 14 */ #define CENTER 1 #define STARSIZE 8 void bad_center() /* pid=1 */ { int msg,pid,i; while(1) { msg=pm_receive(&pid); msg++; pm_putchar('.'); for (i = 2; i <= STARSIZE; i++) { if (i != pid) { pm_send (i, msg++); } } } } void ok_rim() /* pid=2,3,4,5,6,7,8 */ { int msg,pid; char s[MAXLINE]; while(1) { msg=pm_receive(&pid); msg++; pm_putchar('.'); if (pid != CENTER) pm_send(CENTER,msg); else if (msg==STARSIZE) { sprintf(s,"Message finished at: %d",pm_getpid()); pm_putline(s); } } } void bad_init_star() { int pid; pm_create(CENTER,1,bad_center,0); pm_resume(CENTER); for (pid=2; pid<=STARSIZE; pid++) { pm_create(pid,1,ok_rim,0); pm_resume(pid); } } /* ex. 15 */ #define NO_CUSTOMER 0 int true=1; void bad_barber() { char s[MAXLINE]; customer_id = NO_CUSTOMER; barb_id = pm_getpid(); while (true) { pm_putline("barber ready to receive."); sprintf(s,"cutting hair customer : %d",customer_id); pm_putline(s); sprintf(s,"finished with customer: %d",customer_id); pm_putline(s); } pm_receive(&customer_id); pm_send(customer_id,FINISH_MSG); } void bad_customer() { int finish_msg; char s[MAXLINE]; while (true) { sprintf(s,"enter the barber shop : %d",pm_getpid()); pm_putline(s); pm_wait(sem_customer_id); if (customer_id == NO_CUSTOMER) { customer_id = pm_getpid(); customer_id = NO_CUSTOMER; } else pm_signal(sem_customer_id); sprintf(s,"leave the barber shop : %d",pm_getpid()); pm_putline(s); } pm_signal(customer_id); pm_send(barb_id,customer_id); pm_receive(&finish_msg); pm_wait(customer_id); pm_signal(customer_id); pm_sleep(SLEEP); } void bad_init_barbershop() { customer_id=pm_seminit(1); pm_create(1,1,bad_barber,0); pm_create(2,1,bad_customer,0); pm_create(3,1,bad_customer,0); pm_resume(1); pm_resume(2); pm_resume(3); } /* ex. 16 */ #define SHORT 1 #define MEDIUM 2 #define LONG 3 void test_scheduling(HOWLONG) int HOWLONG; { int i,j; for (i=1; i<= 10; i++) { /* 10 cpu bursts */ for (j=1;j<=5*HOWLONG;j++) /* 1 cpu burst */ pm_busywait(); /* pid1: long pid2: medium pid3: short */ pm_yield(); /* go back to ready queue. */ } pm_destroy(pm_getpid()); } void init_scheduling() { pm_create(1,2,test_scheduling,LONG); pm_create(2,3,test_scheduling,MEDIUM); pm_create(3,1,test_scheduling,SHORT); pm_resume(1); pm_resume(2); pm_resume(3); } /* ex. 17 */ void test_utilization() { int i,j; for (i=1; i<=10; i++) { /* 10 cpu bursts */ for (j=1;j<=3;j++) /* 1 cpu burst */ pm_busywait(); pm_sleep(7); } pm_destroy(pm_getpid()); } void init_utilization() { int pid; int n; n=3; /* degree of multiprogramming */ for (pid=1; pid<=n; pid++) { pm_create(pid,1,test_utilization,0); pm_resume(pid); } } /* outfile: prog1.out */ /* there is no output for this program. */
BACK TO CS4560 PAGE.