os lab record final

Upload: aravind-arulanandam

Post on 05-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Os Lab Record Final

    1/47

    ZOMBIE PROCESS

    #includemain(){

    int pid=fork();printf("my numbers is %d",getpid());printf("\n");if(pid>0){printf("my parent id is %d",getpid());sleep(2);}}

    OUTPUT:

    my numbers is 5571my numbers is 5570my parent id is 5570

    1

  • 8/2/2019 Os Lab Record Final

    2/47

    WAIT PROGRAM

    #includemain(){int pid,dip,cpid;pid=fork();if(pid==0){printf("\n first child process id is %d",getpid());printf("\n first child process terminating from memory");}else{dip=fork();if(dip==0){printf("\n second child process id is %d died",getpid());printf("\n second child process terminating from memory");}else{cpid=wait(0);printf("\n child with pid %d died",cpid);cpid=wait(0);printf("\n child with pid %d died",cpid);

    printf("\n parent is ending");}}}

    OUTPUT:

    first child process id is 6476first child process terminating from memory

    second child process id is 6477 diedsecond child process terminating from memorychild with pid 6476 diedchild with pid 6477 died

    2

  • 8/2/2019 Os Lab Record Final

    3/47

    FORK PROGRAM

    #includemain(int argc,char *argv[]){

    int pid;pid=fork();if(pid

  • 8/2/2019 Os Lab Record Final

    4/47

    FIRST COME FIRST SERVE

    #includemain(){

    struct{int id,btime,wtime,totime;}jb[20];int i,j,n,wsum=0,fsum=0;float aw=0.0,at=0.0;printf("enter the number of jobs\n");scanf("%d",&n);printf("\n\t enter the btime for each process");printf("\n\n\tpid\t\tbtime");

    printf("\n*********************************\n");for(i=0;i

  • 8/2/2019 Os Lab Record Final

    5/47

    OUTPUT:

    enter the number of jobs3

    enter the btime for each process

    pid btime*********************************

    1 242 33 3

    ********* first come first serve *************pid btime wtime totime1 24 0 24

    2 3 24 27

    3 3 27 30--------------------------------------------------------------------------

    average waiting time17.00

    average turn around time

    27.00

    5

  • 8/2/2019 Os Lab Record Final

    6/47

    PRIORITY SCHEDULING

    #includemain()

    {struct{int id,btime,wtime,totime,pty;}p[20];int t,i,j,n,wsum=0,fsum=0;float aw=0.0,at=0.0;printf("enter the number of job\n");scanf("%d",&n);printf("\n\tenter the btime & priority for each process");

    printf("\n\n\tpid\t\tbtime\t\tpriority");printf("\n***************\n");for(i=0;i

  • 8/2/2019 Os Lab Record Final

    7/47

    printf("\n\tpid\tbtime\tpty\twtime\ttotime");for(i=0;i

  • 8/2/2019 Os Lab Record Final

    8/47

    SHORTEST JOB FIRST

    #includemain()

    {struct{int id,btime,wtime,totime;}s[20];int i,j,n,wsum=0,fsum=0,t;float aw=0.0,at=0.0;printf("enter the number of job\n");scanf("%d",&n);printf("\n\t enter the btime & priority for each process");printf("\n\n\tpid\t\tbtime");

    printf("\n*********************************************************\n");for(i=0;i

  • 8/2/2019 Os Lab Record Final

    9/47

    fsum=fsum+s[i].totime;s[i+1].wtime=s[i].wtime+s[i].btime;s[i+1].totime=s[i+1].wtime+s[i+1].btime;}for(i=0;i

  • 8/2/2019 Os Lab Record Final

    10/47

    DINING PHILOSOPHER PROBLEM

    #includechar state[10],self[10];void pickup(int);

    void putdown(int);int i,n,a,ch,k;main(){printf("DINING PHILOSOPHER PROBLEM\n");printf("\n___________________\n");for(i=0;i

  • 8/2/2019 Os Lab Record Final

    11/47

    printf("\n enter your choice");scanf("%d",&ch);}}void test(int k)

    {if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e')){state[k]='e';self[k]='s';}}void pickup(int i){state[i]='h';test(i);

    if(state[i]!='e'){self[i]='w';}}void putdown(int i){state[i]='t';test((i+4)%5);test((i+1)%5);}

    OUTPUT:

    DINING PHILOSOPHER PROBLEM

    ___________________initialise state of each philosopher

    0 t s

    1 t s

    2 t s

    3 t s

    4 t sMAIN MENU

    11

  • 8/2/2019 Os Lab Record Final

    12/47

    1.HUNGRY2.THINKING3.EXIT

    enter your choice1

    enter which philosopher is hungry1

    initialize state of each philosopher

    0 t s

    1 e s

    2 t s

    3 t s

    4 t s

    MAIN MENU1.HUNGRY2.THINKING3.EXIT

    enter your choice1enter which philosopher is hungry

    2

    initialize state of each philosopher

    0 t s

    1 e s

    2 h w

    3 t s

    4 t s

    MAIN MENU1.HUNGRY2.THINKING3.EXIT

    12

  • 8/2/2019 Os Lab Record Final

    13/47

    enter your choice1enter which philosopher is hungry3

    initialize state of each philosopher

    0 t s

    1 e s

    2 h w

    3 e s

    4 t s

    MAIN MENU1.HUNGRY2.THINKING3.EXIT

    enter your choice2enter which philosopher want to think1

    initialize state of each philosopher

    0 t s

    1 t s

    2 h w

    3 e s

    4 t s

    MAIN MENU

    1.HUNGRY2.THINKING3.EXIT

    enter your choice3

    13

  • 8/2/2019 Os Lab Record Final

    14/47

    PRODUCER CONSUMER

    #include

    #define SIZE 5int stack[SIZE],top=-1,in=0;void produce();void consume();main(){char ch;printf("\n MENU\n");printf("\np.PRODUCE\n");printf("\nc.CONSUME\n");printf("\nq.QUIT\n");

    while(1){ch=getchar();switch(tolower(ch)){case 'p':produce();break;case 'c':consume();break;

    case 'q':exit(0);}}}void produce(){int value;if(top

  • 8/2/2019 Os Lab Record Final

    15/47

    void consume(){if(top==-1)printf("cannot consume");else

    printf("consume: %d",stack[top--]);}

    OUTPUT:

    MENU

    p.PRODUCE

    c.CONSUME

    q.QUITpproduce: 1pproduce: 2pproduce: 3pproduce: 4p

    produce: 5pcannot producecconsume: 5cconsume: 4cconsume: 3cconsume: 2c

    consume: 1ccannot consumeq

    15

  • 8/2/2019 Os Lab Record Final

    16/47

    READ SYSTEM CALL

    #include#include

    main(int argc,char * argv[]){int fd,i;char ch[1];if(argc0)printf("%c",ch[0]);close(fd);}}

    OUTPUT:

    CONTENT of the file red.c:#includemain(){if(fork()==0){system("clear");printf("child:i am child number is %d\n",getpid());printf("child:my parent number is %d\n",getpid());

    }else{printf("parent:i am parent %d\n",getpid());sleep(2);printf("parent:my parent is %d\n",getpid());}}

    16

  • 8/2/2019 Os Lab Record Final

    17/47

    WRITE SYSTEM CALL

    #include#include

    #includemain(int argc,char *argv[]){int fd,i;char ch[1];if(argc0)scanf("%c",&ch[0]);close(fd);printf("THE CONTENT IN THE FILE is");}}

    OUTPUT:

    ^C^@h^C^@a^C^@i^C^@ C^@h^C^@o^C^@w^C^@ ^C^@r C^@ C^@u^C^@C^@/^C^@C^@e^C^@n^C^@d^C^@

    17

  • 8/2/2019 Os Lab Record Final

    18/47

    LS SIMULATION CALL

    #include#includeint main()

    {struct dirent **namelist;int n,i;char pathname[100];getcwd(pathname);n=scandir(pathname,&namelist,0,alphasort);if(n

  • 8/2/2019 Os Lab Record Final

    19/47

    STAT SYSTEM CALL

    #includeint main(){

    struct stat fp;if(stat("/etc/passwd",&fp)==-1){printf("error");exit(0);}printf("inode:%d\n",fp.st_ino);printf("type&permission:%d\n",fp.st_nlink);printf("userid:%d\n",fp.st_uid);printf("groupid:%d\n",fp.st_gid);printf("deviceid:%d\n",fp.st_rdev);

    printf("previous access time:%d\n",fp.st_atime);printf("modification time:%d\n",fp.st_mtime);exit(0);}

    OUTPUT:

    inode:1653738type&permission:1userid:0

    groupid:0deviceid:0previous access time:1329466058modification time:1328762930

    19

  • 8/2/2019 Os Lab Record Final

    20/47

    CREATE SYSTEM CALL

    #include#include

    #includeint main(){int fd;fd=creat("datafile.dat",S_IREAD|S_IWRITE);if(fd==-1)printf("error in opening datafile.dat\n");else{printf("datafile.dat opened for read/write access\n");printf("datafile.dat is currently empty");

    }close(fd);exit(0);}

    OUTPUT:

    datafile.dat opened for read/write accessdatafile.dat is currently empty

    20

  • 8/2/2019 Os Lab Record Final

    21/47

    OPENDIR CLOSEDIR AND READDIR SYSTEM CALL

    #include#include

    int main(int argc,char *argv[]){DIR *dirEntry;struct dirent *de;dirEntry=opendir("/home/student");while((de=readdir(dirEntry))!=NULL)printf("%d\t%s\n",de->d_ino,de->d_name);closedir(dirEntry);exit(0);}

    OUTPUT:1368142 sunday.c1366182 .difcfs.c.swp1365887 vcv.c1365921 aparnaa.c1366546 dinshortestjob.c1367533 prishe.sh1366083 .559.swp1365994 bumeena.c1368063 harisshh.c1366317 dinfcfs.cclear

    1367816 ss.c1365625 mos.c1365832 .fes.c.swp1365819 fcfs460.c1365671 .kkkkk.c.swp1368193 fcfs1.c1365933 fifs.c1368166 .firstcome1.c.swo1366351 .ICEauthority1365987 dinning.c1366247 alclr

    1367239 .fz.c.swo1366649 Hudson1365831 .kebin.sh.swp1366922 .muthu.sh.swp1365799 .har.c.swp1366204 .mari.c.swp1367185 vrevu.c1366184 .wait.swp

    21

  • 8/2/2019 Os Lab Record Final

    22/47

    PAGING

    #includeint i,j,k,ps,np1,op,np,np2,fn,f=0;char p1[50][50],p2[50][50];int pgtb(int r,int fn,int np){for(i=0;i

  • 8/2/2019 Os Lab Record Final

    23/47

    printf("\n PROCESS 1");printf("\n........\n");p1[np1][ps];p2[np2][ps];for(i=0;i

  • 8/2/2019 Os Lab Record Final

    24/47

    printf("%d th frame",i);break;case 5:return(0);break;

    }}}

    OUTPUT:

    enter the page size:10

    enter the no of frames:3enter the no of pages of process 1:2

    enter the no of pages of process 2:3

    page fault will occur

    PROCESS 1........

    enter CHAR for page 0:a

    enter CHAR for page 1:b

    PROCESS 2............

    enter CHAR for page 0:c

    enter CHAR for page 1:d

    enter CHAR for page 2:e

    1.page table for PROCESS 12.page table for PROCESS 2

    3.frame allotment4.free frame list5.exit

    enter ur choice1

    page table for PROCESS 1:PAGE INDEX FRAME INDEX

    24

  • 8/2/2019 Os Lab Record Final

    25/47

    0 01 1

    1.page table for PROCESS 12.page table for PROCESS 2

    3.frame allotment4.free frame list5.exit

    enter ur choice2

    page table for PROCESS 2:PAGE INDEX FRAME INDEX

    0 21 3

    2 41.page table for PROCESS 12.page table for PROCESS 23.frame allotment4.free frame list5.exit

    enter ur choice3

    .......................

    frame no 0a

    .......................

    frame no 1bc

    1.page table for PROCESS 12.page table for PROCESS 2

    3.frame allotment4.free frame list5.exit

    enter ur choice4page fault has occured..........

    25

  • 8/2/2019 Os Lab Record Final

    26/47

    1.page table for PROCESS 12.page table for PROCESS 23.frame allotment4.free frame list5.exit

    enter ur choice5

    26

  • 8/2/2019 Os Lab Record Final

    27/47

    PARENT CHILD RELATIONSHIP

    #includemain()

    {if(fork()==0){system("clear");printf("child:i am child number is %d\n",getpid());printf("child:my parent number is %d\n",getpid());}else{printf("parent:i am parent %d\n",getpid());sleep(2);

    printf("parent:my parent is %d\n",getpid());}}

    OUTPUT:

    child:i am child number is 10836parent:i am parent 10835child:my parent number is 10836parent:my parent is 10835

    27

  • 8/2/2019 Os Lab Record Final

    28/47

    FIRST FIT

    #includestruct memblock{

    char name[20];int size;int status;}m[20];struct process{char pro[20];int pro_size;}p[20];main(){

    int n,j,i;struct memblock m1[20];system("clear");printf("\n enter the no of processes");scanf("%d",&n);for(i=1;i

  • 8/2/2019 Os Lab Record Final

    29/47

    for(i=1;i

  • 8/2/2019 Os Lab Record Final

    30/47

    WORST FIT

    #includestruct memblockint size;

    int status;}m[20];struct process{char pro[20];int pro_size;}p[20];main(){int n,j,i,temp;struct memblock m1[20],w[20];

    system("clear");printf("enter the no of process");scanf("%d",&n);for(i=1;i

  • 8/2/2019 Os Lab Record Final

    31/47

    if(p[i].pro_size

  • 8/2/2019 Os Lab Record Final

    32/47

    BLOCK1 86 ALLOCATED

    BLOCK2 83 ALLOCATED

    BLOCK3 77 FREE

    BLOCK4 15 FREE

    32

  • 8/2/2019 Os Lab Record Final

    33/47

    IPC MESSAGE QUEUE

    #include#include

    #include#includeint main(){int pid,len,qid;struct{long mtype;char mtext[10];}message;system("clear");

    printf("\n inter process communication using message queue\n");qid=msgget((key_t)13,IPC_CREAT|0666);printf("the message queue is created\n");if(qid==-1){printf("message queue is not created\n");exit(1);}printf("the value of qid is %d",qid);strcpy(message.mtext,"priya");message.mtype=1;

    len=strlen(message.mtext);pid=fork();printf("\n the value of pid is:%d\n",pid);if(pid==0){msgsnd(qid,&message,len,IPC_NOWAIT);printf("\n message send\n");}if(pid>0){msgrcv(qid,&message,strlen(message.mtext),0,IPC_NOWAIT|MSG_NOERROR);

    printf("\n message is %s\n",message.mtext);printf("\n message is received\n");}if(pid==-1){printf("error in creating a child\n");exit(1);}

    33

  • 8/2/2019 Os Lab Record Final

    34/47

    }

    OUTPUT:inter process communication using message queue

    the message queue is createdthe value of qid is 0the value of pid is:0

    message sendthe value of qid is 0the value of pid is:12096

    message is priya

    message is received

    34

  • 8/2/2019 Os Lab Record Final

    35/47

    INTERPROCESS COMMUNICATION USING PIPES

    #include#include

    #define MAXSIZE 40main(){char*m1="hai";char inbuf[MAXSIZE];int p[2],pid;system("clear");printf("ipc using pipes\n");pipe(p);pid=fork();printf("the value of pid is %d\n",pid);

    if(pid==-1){printf("error\n");exit(1);}if(pid==0){printf("\n child process is writing\n");write(p[7],m1,MAXSIZE);printf("\n parent is reading the msg from pipe sent by child using fd[0]\n");read(p[0],inbuf,MAXSIZE);

    printf("\n the msg read is %s\n",inbuf);exit(0);}}

    OUTPUT:

    ipc using pipesthe value of pid is 0

    child process is writing

    parent is reading the msg from pipe sent by child using fd[0]the value of pid is 12875

    35

  • 8/2/2019 Os Lab Record Final

    36/47

    BEST FIT

    #include#include#include

    struct segment{char jobid[10];int size;char status[10];}s[10];char progid[20][20];\int n,v,t,pos,i,j,progsize[10],val,ch;int totmem=0,maxmem=0,sn=0;main(){

    system("clear");menu();for(i=0;i

  • 8/2/2019 Os Lab Record Final

    37/47

    for(i=0;i

  • 8/2/2019 Os Lab Record Final

    38/47

    BLOCK4 93 FREE

    BLOCK5 35 FREE

    BLOCK6 86 FREE

    BLOCK7 92 FREE

    BLOCK8 49 FREE

    BLOCK9 21 FREEenter the no of job you want to store3enter the id and size of the program180enter the id and size of the program2

    10enter the id and size of the program319

    BLOCK SIZE STATUS JOB

    BLOCK15 134517072 2BLOCK21 134517100 3BLOCK35 134517128 NULLBLOCK49 134517156 NULLBLOCK77 134517184 NULL

    BLOCK83 134517212 1BLOCK86 134517240 NULLBLOCK86 134517268 NULLBLOCK92 134517296 NULLBLOCK93 134517324 NULL

    38

  • 8/2/2019 Os Lab Record Final

    39/47

    ROUND ROBIN ALGORITHM

    #includemain()

    {int i,j=0,m,n,b[20],b1[20],f[20],w[20],tat[20],start,finish,total=0,t;float awt=0.0,atat=0.0;system("clear");printf("\n enter no of process");scanf("%d",&n);printf("enter the time quantum");scanf("%d",&t);for(i=1;i

  • 8/2/2019 Os Lab Record Final

    40/47

    }}printf("\n\nJOB\tSERVICE TIME\t TURNAROUND TIME\tWAITING TIME");for(i=1;i

  • 8/2/2019 Os Lab Record Final

    41/47

    GREP SIMULATION PROGRAM

    #include

    int main(int argc,char *argv[]){FILE *f1;char c,s[50];int i=0,k=0,cmp=0;f1=fopen(argv[2],"r");if(f1==NULL)printf("file doesnot exit");else{while(!feof(f1))

    {fscanf(f1,"%c",&c);s[i]=c;if(c!='\n'){if(cmp==strlen(argv[1]))i++;else if(c==argv[1][k]){cmp++;k++;

    i++;}else{k=0;cmp=0;i++;}}else{

    s[i]='\0';if(cmp==strlen(argv[1]))fprintf(stdout,"%s\n",s);strcpy(s," ");i=0;k=0;cmp=0;}

    41

  • 8/2/2019 Os Lab Record Final

    42/47

    }}}

    OUTPUT:

    Hai

    42

  • 8/2/2019 Os Lab Record Final

    43/47

    FILE ALLOCATION USING LINKED LIST

    #include#include#include

    void print(int);struct{char data[10];int link;}fat[20];int table[5];main(){int fid;char ch;

    strcpy(fat[0].data,"hello");fat[0].link=2;strcpy(fat[1].data,"wat");fat[1].link=3;strcpy(fat[2].data,"hello");fat[2].link=4;strcpy(fat[3].data,"my");fat[3].link=5;strcpy(fat[4].data,"is");fat[4].link=9;strcpy(fat[5].data,"karthik");

    fat[5].link=6;strcpy(fat[6].data,"EOF");fat[6].link=0;strcpy(fat[7].data,"SELVA");fat[7].link=8;strcpy(fat[8].data,"EOF");fat[8].link=0;strcpy(fat[9].data,"is");fat[9].link=7;strcpy(fat[10].data,"hello");fat[10].link=11;

    strcpy(fat[11].data,"do you");fat[11].link=12;strcpy(fat[12].data,"know me");fat[12].link=8;table[0]=0;table[1]=1;table[2]=10;while(1)

    43

  • 8/2/2019 Os Lab Record Final

    44/47

    {printf("enter the file id");scanf("%d",&fid);print(fid);printf("continue......(y/n)");

    scanf("%s",&ch);if((ch=='n')||(ch=='N'))break;}}void print(int fid){int start;start=table[fid];while(1){

    if(strcmp(fat[start].data,"EOF")==0)break;printf("%s",fat[start].data);start=fat[start].link;}printf("\n");}

    OUTPUT:

    enter the file id0

    hellohelloisisSELVAcontinue......(y/n)yenter the file id3hellohelloisisSELVAcontinue......(y/n)yenter the file id2hellodo youknow mecontinue......(y/n)n

    44

  • 8/2/2019 Os Lab Record Final

    45/47

    IPC USING SHARED MEMORY

    #include#include#include#include#include#include#define MAX_TEXT 512#define BUF 50struct my_msg_st{long int my_msg_type;char some_text[BUF];};int main(){int running=1,msgid;struct my_msg_st some_data;char buffer[BUF];msgid=msgget((key_t)123,0666|IPC_CREAT);if(msgid==-1){printf(stderr,"msgget failed\n");exit(0);}

    while(running){printf("enter some text:");fgets(buffer,BUF,stdin);some_data.my_msg_type=1;strcpy(some_data.some_text,buffer);if(msgsnd(msgid,(void *)&some_data,MAX_TEXT,0)==-1){printf(stderr,"msgsnd failed with error\n");exit(0);}

    if(strncmp(buffer,"end",3)==0){running=0;}}exit(1);}

    45

  • 8/2/2019 Os Lab Record Final

    46/47

    OUTPUT:

    enter some text:my brothersenter some text:naveen

    enter some text:prabhuenter some text:kevinenter some text:end

    46

  • 8/2/2019 Os Lab Record Final

    47/47

    CLIENT SERVER

    #include#include#include

    #includestruct my_msg_st{long int my_msg_type;char some_text[BUFSIZ];};int main(){int running=1,msgid;struct my_msg_st some_data;long int msg_to_receive=0;

    msgid=msgget((key_t)123,0666|IPC_CREAT);if(msgid==-1){printf(stderr,"msgget failed\n");exit(0);}while(running){if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1){fprintf(stderr,"msgnd failed with error\n");

    exit(0);}printf("you wrote:%s",some_data.some_text);if(strncmp(some_data.some_text,"end",3)==0){running=0;}}exit(1);}

    OUTPUT:

    you wrote:my brothersyou wrote:naveenyou wrote:prabhuyou wrote:kevin