lab(10-09-14)

Upload: ashivaramakrishna

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 lab(10-09-14)

    1/27

    Experiment 31 : IMPLEMENTATION OF ECHO SERVER USINGSHARED MEMORY

    AIM: To write a C program to Implement Echo Server Using Shared Memory

    THEORY:

    A section of shared memory is similar to message queue or pipe, and we havethe same naming operations.

    1) system-assigned names that are returned by a system call and must be passedto other processes by some form of IPC or to child process.

    2) User-assigned names that are arbitary integers. The sharing processes mustagree somehow on the identifier. (used in Unix shared memory system calls)

    3) Shared segments that are named by the file-naming system.

    int shmget(key_t key, int size, int shmflag); A shared memory segment is created or an existing one is accessed with theshmget system call.The value returned by shmget is the shared memoryidentifier, shmid or1 if an error occurs.char shmat(int shmid, char *shmaddr, int shmflag);This system call provides access to the segment for the calling process.It returns

    the starting address of the shared memory segment.The shmsddr argument iszero, the system selects the address for the caller.By default, the shared memorysegment is attached for both reading and writing by the calling process(specifiedby flag zero)

    int shmdt(char *shmaddr);When a process is finished with the shared memory segment, it detaches thesegment by calling the shmdt system call.This call only detaches but does notdelete the shared memory segment.

    Echo ServerREQUIREMENTS:

  • 8/10/2019 lab(10-09-14)

    2/27

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    ALGORITHM :

    STEP 1: Shared memory is created using shmget system call.STEP 2: The shared memory is attached to a character pointer and data is readinto it from keyboard in server process and then detachedSTEP 3: The shared memory is attached to character pointer and data is writtenfrom the shared memory onto client process and then detached

    PROGRAM:

    // File 1: Shm.h

    #include #include #include #include

    #define PERMS 0666#define SHMKEY0 ((key_t) 5444)#define SHMKEY1 ((key_t) 5445)

    // File 2: smop.h#include#include#include#define SEMKEY 1234L#define PERMS 0666static struct sembuf lock0[2]={

    0,0,0,0,1,0

    };static struct sembuf unlock0[1]={

    0,-1,IPC_NOWAIT};static struct sembuf lock1[2]=

  • 8/10/2019 lab(10-09-14)

    3/27

    {1,0,0,1,1,0

    };static struct sembuf unlock1[2]={

    1,-1,IPC_NOWAIT};static struct sembuf lock2[2]={

    2,0,0,2,1,0

    };static struct sembuf unlock2[1]={

    2,-1,IPC_NOWAIT};int semlock0(int semid){

    semop(semid,&lock0[0],2);}int semunlock0(int semid){

    semop(semid,&unlock0[0],1);}

    int semlock1(int semid){

    semop(semid,&lock1[0],2);}int semunlock1(int semid){

    semop(semid,&unlock1[0],1);}int semlock2(int semid){

    semop(semid,&lock2[0],2);}

    int semunlock2(int semid){

    semop(semid,&unlock2[0],1);}

  • 8/10/2019 lab(10-09-14)

    4/27

  • 8/10/2019 lab(10-09-14)

    5/27

  • 8/10/2019 lab(10-09-14)

    6/27

    {perror("\n Server : Can't detach shared memory");exit(1);

    }if( (shmdt(pshm1))

  • 8/10/2019 lab(10-09-14)

    7/27

    THEORY:

    The dining-philosophes problem is considered a classic synchronizationproblem, because it is an example for a large class of concurrency-controlproblems.It is a simple representation of the need to allocate several resourcesamong several processes in a deadlock and starvation free manner.

    One simple solution is to represent each chopstick by a semaphore.AphilosopherTries to grab the chopstick by executing a wait operation on that semaphore; shereleases her chopstick by executing the signal operation on the appropriatesemaphores.Thus the shared data are

    var chopstick:array[0..4] of semaphore

    where all the elements of the chopstick are initialized to 1.The structure ofphilosopher i is shown below.

    repeatWait(chopstick[i]);Wait(chopstick[i+1 mod 5]); eat signal(chopstick[i]);signal(chopstick[i+1 mod 5]]); think

    until false;

  • 8/10/2019 lab(10-09-14)

    8/27

  • 8/10/2019 lab(10-09-14)

    9/27

    semlock2(semid);

    printf("\n\n\tPhilosopher %d eating...",pno);printf("\n\tPress 1 to stop eating ...");scanf("%d",&tmp);

    semunlock1(semid);semunlock2(semid);

    printf("\n\n\tPhilosopher %d thinking...",pno);break;

    case 2: printf("\n\n\tPhilosopher %d thinking...",pno);printf("\n\tEnter 0 to start eating ...");scanf("%d",&tmp);

    semlock2(semid);semlock0(semid);

    printf("\n\n\tPhilosopher %d eating...",pno);printf("\n\tPress 1 to stop eating ...");scanf("%d",&tmp);

    semunlock2(semid);semunlock0(semid);

    printf("\n\n\tPhilosopher %d thinking...",pno);break;

    }}

    OUTPUT:

    [vasavi-edu david]$cc dinnin.c -op[vasavi-edu david]$./penter philosopher(0- 2)0 philosopher 0 thinking enter 0 to start eating0philosopher 0 eating enter 1 to stop eating[vasavi-edu david]$cc dinin.c -op[vasavi-edu david]$./penter philosopher number(0-2)1

  • 8/10/2019 lab(10-09-14)

    10/27

  • 8/10/2019 lab(10-09-14)

    11/27

  • 8/10/2019 lab(10-09-14)

    12/27

    wait(mutex);readcount:=readcount-1;if (readcount=0 then signal(wrt);signal(mutex);

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    PROGRAM:// File 1: smop.h#include#include#include

    #define SEMKEY 223010L#define PERMS 0666static struct sembuf lock0[2]={

    0,0,0,0,1,0

    };static struct sembuf unlock0[1]={

    0,-1,IPC_NOWAIT

    };static struct sembuf lock1[2]={

    1,0,0,1,1,0

    };static struct sembuf unlock1[1]={

    1,-1,IPC_NOWAIT};static struct sembuf lock2[2]={

    2,0,0,2,1,0

    };static struct sembuf unlock2[1]={

    2,-1,IPC_NOWAIT

  • 8/10/2019 lab(10-09-14)

    13/27

    };int semlock0(int semid){

    semop(semid,&lock0[0],2);}int semunlock0(int semid){

    semop(semid,&unlock0[0],1);}int semlock1(int semid){

    semop(semid,&lock1[0],2);}int semunlock1(int semid)

    { semop(semid,&unlock1[0],1);}int semlock2(int semid){

    semop(semid,&lock2[0],2);}int semunlock2(int semid){

    semop(semid,&unlock2[0],1);

    } / /File 2: Reader.c#include #include "smop.h"#define nreaders 2main(){

    int semid=semget(SEMKEY,2,PERMS|IPC_CREAT);int rno,tmp;printf("\n\n\t\tEnter Reader Number : ");scanf("%d",&rno);printf("\n\n\t\tPress 0 to start reading: ");scanf("%d",&tmp);switch(rno){

    case 0: semlock0(semid);break;

    case 1: semlock1(semid);

  • 8/10/2019 lab(10-09-14)

    14/27

    break;}printf("\n\n\t\t%d Reader reading.....",rno);printf("\n\t\tEnter 1 to stop reading");scanf("%d",&tmp);switch(rno){

    case 0: semunlock0(semid);break;

    case 1: semunlock1(semid);break;

    }}/ /File 3: Writer.c#include

    #include "smop.h"main(){

    int semid=semget(SEMKEY,2,PERMS|IPC_CREAT);int tmp;printf("\n\n\t\tEnter 0 to start writing : ");scanf("%d",&tmp);semlock0(semid);semlock1(semid);printf("\n\n\t\t Writer writing.....");

    printf("\n\t\tEnter 1 to stop writing: ");scanf("%d",&tmp);semunlock0(semid);semunlock1(semid);

    }

    OUTPUT:[vasavi-edu]$ cc reader.c -op[vasavi-edu]$ ./penter reader number :1enter 0 to start reading :01 reader reading enter 1 to stop reading:[vasavi-edu ]$cc writer.c -op[vasavi-edu]$./penter 0 to start writiing:01[vasavi-edu]$ writer writing

  • 8/10/2019 lab(10-09-14)

    15/27

  • 8/10/2019 lab(10-09-14)

    16/27

    EXPERIMENT 34: IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM USINGSEMAPHORES

    AIM: To write a C program to Implement Producer Consumer Problem UsingSemaphores.

    THEORY :

    The Producer-consumer relationship is the most basic pattern of cooperativeinter- process communication.Producer produces full buffers for the consumerand the consumer produces empty buffers for the producer.We assume that thepool consists of n buffers, each capable of holding one item.The mutexsemaphore provides mutual exclusion for accesses to the buffer pool and isinitialized to the value 1.The empty and full semaphores count the number of fulland empty buffers respectively.The semaphore empty is initialized to value n;the semaphore full is initialized to the value 0.

    Figure illustrating Producer consumer problem implementation using semaphoreThe structure of the producer processrepeat

    produce an item in nextp wait(empty);wait(mutex);

    add nextp to buffer signal(mutex);signal(full);

    until false

    The structure of the consumer process

  • 8/10/2019 lab(10-09-14)

    17/27

    repeatWait(full);Wait(mutex); remove an item from buffer to nextc signal(mutex);signal(empty); consume the item in nextc

    until false;

    REQUIREMENTS:

    HARDWARE : PIII Processor , 128 MB RAM, 10GBSOFTWARE : OS: LINUX

    PROGRAM// File 1: smop.h

    #include#include#include#define SEMKEY 99990L

    #define PERMS 0666static struct sembuf lock0[2]={

    0,0,0,0,1,0

    };static struct sembuf unlock0[1]={

    0,-1,IPC_NOWAIT};static struct sembuf lock1[2]={

    1,0,0,1,1,0

    };static struct sembuf unlock1[1]={

    1,-1,IPC_NOWAIT

  • 8/10/2019 lab(10-09-14)

    18/27

    };static struct sembuf lock2[2]={

    2,0,0,2,1,0

    };static struct sembuf unlock2[1]={

    2,-1,IPC_NOWAIT};

    int semlock0(int semid){

    semop(semid,&lock0[0],2);}

    int semunlock0(int semid){semop(semid,&unlock0[0],1);

    }int semlock1(int semid){

    semop(semid,&lock1[0],2);}int semunlock1(int semid){

    semop(semid,&unlock1[0],1);}int semlock2(int semid){

    semop(semid,&lock2[0],2);}int semunlock2(int semid){

    semop(semid,&unlock2[0],1);}

  • 8/10/2019 lab(10-09-14)

    19/27

    // File 2: Producer.c

    #include #include "smop.h"main(){

    int semid=semget(SEMKEY,3,PERMS|IPC_CREAT);int tmp,flag=1;semlock1(semid);while(flag != 2){

    printf("\n\n\t\tEnter 0 to start produce:");scanf("%d",&tmp);semlock0(semid);semlock2(semid);

    printf("\n\t\tProducer producing...");printf("\n\t\tEnter 1 to stop :");scanf("%d",&tmp);semunlock2(semid);semunlock1(semid);printf("\n\n\t\tProducer stop producing");printf("\n\n\t\tEnter 2 to exit...\n Any other to continue");scanf("%d",&flag);

    }}

  • 8/10/2019 lab(10-09-14)

    20/27

    // File 2: Consumer.c

    #include #include "smop.h"main(){

    int semid=semget(SEMKEY,3,PERMS|IPC_CREAT);int tmp,flag=1;while(flag != 2){

    printf("\n\n\t\tEnter 0 to start consume");scanf("%d",&tmp);semlock1(semid);semlock2(semid);printf("\n\t\tConsumer Consuming ...");printf("\n\t\tEnter 1 to stop :");scanf("%d",&tmp);semunlock2(semid);semunlock0(semid);printf("\n\n\t\tConsumer stop consuming");printf("\n\n\t\tEnter 2 to exit...\n Any other to continue");scanf("%d",&flag);

    }}

  • 8/10/2019 lab(10-09-14)

    21/27

    OUTPUT:

    [vasavi-edu david]$cc producer.c -op[vasavi-edu david]$./penter 0 to produce:0produc er producing enter 1 to stop :[vasavi-edu david]$cc consumer.c -op[vasavi-edu david]$./penter 0 to consume:01producer stop producingconsumer consuming enter 2 to exit:enter 1 to stop:1

    any other key to continue:2consumer stop consuming[vasavi-edu david]$enter 2 to exitany other key to continue:2

    Result: A C program to Implement Producer Consumer Problem Using Semaphores isexecuted successfully.

  • 8/10/2019 lab(10-09-14)

    22/27

  • 8/10/2019 lab(10-09-14)

    23/27

    #include#includeint s,w;pthread_t tid,t2id;void *create(void *arg){

    printf("The thread with id %d is created\n",pthread_self());}void thid(void){

    pthread_t tid;tid=pthread_self();printf("The main thread has an id=%d\n",tid);

    }void equal(void)

    { int e;e=pthread_equal(t2id,tid);if(e==0)

    printf("Threads are not equal\n");else

    printf("Threads are equal\n");}void detach(void){

    int x;x=pthread_detach(tid);if(x==0)

    printf("Successfully detached thread with the id %d\n",tid);else

    printf("Thread is not detached\n");}void joint(void){

    int c;int *code;c=pthread_join(t2id,(void**)&code);if(c==0)

    printf("Joined back thread with the id=%d\n",t2id);else

    printf("Could not join\n");}int main(void)

  • 8/10/2019 lab(10-09-14)

    24/27

    {int i,a,b;a=pthread_create(&tid,NULL,create,&s);b=pthread_create(&t2id,NULL,create,&w);while(1){

    printf("Which one u want\n");printf("1.Thread id\n");printf("2.Join:\n");printf("3.Test for equality\n");printf("4.Detach a thread\n");printf("5.Exit\n");scanf("%d",&i);switch(i){

    case 1:thid();break;

    case 2: joint();break;

    case 3:equal();break;

    case 4:

    detach();break;case 5:

    pthread_exit(NULL);}

    }}OUTPUT:

    [david@vasavi-edu os-lab]$ cc -o prg18.o prg18.c -pthread[david@vasavi-edu os-lab]$ ./prg18.oThe thread with id 1026 is createdThe thread with id 2051 is createdWhich one u want1.Thread id2.Join:3.Test for equality4.Detach a thread

  • 8/10/2019 lab(10-09-14)

    25/27

    5.Exit1The main thread has an id=1024 Which one u want1.Thread id2.Join:3.Test for equality4.Detach a thread5.Exit2 Joined back thread with the id=2051Which one u want1.Thread id2.Join:3.Test for equality

    4.Detach a thread5.Exit3Threads are not equalWhich one u want1.Thread id2.Join:3.Test for equality4.Detach a thread5.Exit

    5[david@vasavi-edu os-lab]$

    Result: A program in c to illustrate the thread management functions isimplemented successfully.

  • 8/10/2019 lab(10-09-14)

    26/27

  • 8/10/2019 lab(10-09-14)

    27/27

    #includevoid *proc(void * arg){

    printf("The thread created with the id : %d\n",pthread_self());}int main(void){

    pthread_t tid;int fd,i,m,y,val;pthread_attr_t attr;void *proc(void *arg);fd=open("abc",O_RDONLY);i=pthread_attr_init(&attr);if(i==-1)

    printf("Failed to initialized\n");

    else printf("Default values are initialized\n");m=pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);if(m==-1)

    printf("Failed to set state of the attribute\n");else

    printf("Successful to set state of attribute\n");y=pthread_create(&tid,&attr,proc,&fd);if(y==-1)

    printf("Failed to create\n");

    else printf("Thread created\n");m=pthread_attr_getdetachstate(&attr,&val);printf("Thread state is %x\n",val);

    }OUTPUT:

    [david@vasavi-edu os-lab]$ cc -o prg19.o prg19.c -pthread[david@vasavi-edu os-lab]$ ./prg19.oDefault values are initializedSuccessful to set state of attributeThe thread created with the id : 1026Thread createdThread state is 1

    Result: A program in C to illustrate the attributes of states of a thread isimplemented successfully.