OS Programs for Record

Embed Size (px)

Citation preview

  • 8/8/2019 OS Programs for Record

    1/71

    STUDY OF BASIC COMMANDS OF UNIX

    COMMANDS:

    CAL:

    DESCRIPTION:

    The cal command is used to display the calendar.

    SYNTAX:

    [$ CAL YEAR]

    DATE:

    DESCRIPTION:

    The date command is used to display the day,month,year and time.

    SYNTAX:

    [$DATE]

    The command can also be used within suitable format specified as argument .

    Each format specified as argument .Each format is prescribed by a symbol followed by the %

    Operator describing the format,

    -m -> Display the month in number

    -h -> Display the month in words

    -d -> Display the day in words

    -y -> Display last two digits of the year

    OPTIONS:

    U print or sets coordinate universal time

    S set the current time to specified time

    MAN:

  • 8/8/2019 OS Programs for Record

    2/71

    DESCRIPTION:

    Format and displays online manuals.

    SYNTAX:

    Man [option] name

    CLEAR:

    DESCRIPTION:

    Clear the terminal screen .

    WHO:

    DESCRIPTION:

    Displays the information of logged in user.

    OPTIONS:

    -a-> All

    -b-> Time of the last symbol boot

    -d->Prints dead processor

    WHO AM I:

    DESCRIPTION:

    Show the login of the current user .

    PASSWD:

    DESCRIPTION:

    Update on users authentication token.

    SYNTAX:

    Passwd [*][-1][-v][-f][-d][-n mindays][-m maxdays][username]

    OPTIONS:

    -k-> Used to lock an account

    -n->Will set minimum password time

    LOGOUT:

  • 8/8/2019 OS Programs for Record

    3/71

    DESCRIPTION:

    Exit off from current login.

    FILE COMMANDS:

    pwd:

    DESCRIPTION:

    Points the name of the current working directory

    OPTIONS:

    -help->displays help

    mkdir:

    DESCRIPTION:

    Used to make/create the new directory.

    SYNTAX:

    mkdir [option] directory

    OPTIONS:

    -m ->sets permission mode]

    cd:

    DESCRIPTION:

    Change the current directory to specified path.

    SYNTAX:

    cd [directory]

    rmdir:

    DESCRIPTION:

    Used to remove/delete the empty directories.

    SYNTAX:

    rmdir [option]directory]

  • 8/8/2019 OS Programs for Record

    4/71

    OPTIONS:

    -output->diagnostics from each step

    ls:

    DESCRIPTION:

    Used to list directory contents.

    SYNTAX:

    ls [option][file]

    OPTIONS:

    -a-> do not hide entries starting with author of each

    -c-> list entries by columns

    -d-> list directory entries

    -f-> do not sort

    -l-> use long history format

    -r->recursive list

    -s->sort by size

    -v->sort by version

    cat:

    DESCRIPTION:

    Concatenate files and prints on the standard output.

    SYNTAX:

    cat [option] [file]

    OPTION:

  • 8/8/2019 OS Programs for Record

    5/71

    -a->show all

    -n->number of all output lines

    -t->show tabs

    cp:

    DESCRIPTION:

    copies files and directories.

    SYNTAX:

    cp [option] source text

    OPTIONS:

    -r->copy recursively

    -u->copy only when the source is never

    rm:

    DESCRIPTION:

    Removes files (or) directories.

    SYNTAX:

    rm[option]..file..

    OPTION:

    -f->never prompt

    -r->recursive remove

    -i->interactive operation

    mv:

    DESCRIPTION:

    Move or remove files.

    SYNTAX:

  • 8/8/2019 OS Programs for Record

    6/71

    mv[options]..source text

    OPTION:

    -f->do not prompt

    -u->update

    -i->interactive operation

    file:

    DESCRIPTION:

    Determines the file type.

    SYNTAX:

    file[-f file name]

    wc:

    DESCRIPTION:

    Prints the number of bytes,words&lines.

    SYNTAX:

    wc[options][file]

    OPTION:

    -c-> prints the character count

    -w->prints word count

    -l-> print the line count

    head:

    DESCRIPTION:

    Gives the first part of the file.

    SYNTAX:head[options]..[file]

  • 8/8/2019 OS Programs for Record

    7/71

    OPTIONS:

    -c->print first N bytes

    -n->print first n lines

    tail:

    DESCRIPTION:

    Gives the last part of the file.

    SYNTAX:

    tail[option]..[file]

    OPTIONS:

    -c->print last N bytes

    -n->print last N lines

    GREATEST OF THREE NUMBERS

    PROGRAM :

    echo enter a value:read a

    echo enter b value:

    read b

    echo enter c value:

    read c

    if[$a-gt $b-a $a-gt $c]

    thenecho a=$a is greatest

    elif[$b-gt$c]

  • 8/8/2019 OS Programs for Record

    8/71

    then

    echo b=$b is greatest

    elseecho c=$c is greatest

    fi

    OUTPUT :

    Enter a value:3

    Enter a value:2

    Enter c value:5

    C=5 is greatest

  • 8/8/2019 OS Programs for Record

    9/71

    ARITHMETIC OPERATION USING SWITCH CASE

    PROGRAM :

    echo 1.add

    echo 2.sub

    echo 3.mul

    echo 4.div

    echo enter your choice:

    read n

    echo enter a value:

    read a

    echo enter b value:

    read b

    case $ n in

  • 8/8/2019 OS Programs for Record

    10/71

    1)c=expr $a+$b;;

    2) c=expr $a-$b;;

    3) c=expr $a*$b;;

    4) c=expr $a/$b;;

    esac

    OUTPUT :

    1.add

    2.sub

    3.mul

    4.div

    Enter your choice:3

    Enter a value:3

    Enter b value:5

    The result is 15

  • 8/8/2019 OS Programs for Record

    11/71

    STUDENT DETAILS USING WHILE LOOP

    PROGRAM :

    echo enter the no of students:

    read n

    count=1

    while[$count-le$n]

    do

    echo enter the name:

    read name

    echo enter the no:

    read no

    echo enter the marks m1,m2,m3:

    read m1

    read m2

    read m3

  • 8/8/2019 OS Programs for Record

    12/71

    tot=explr $m1+$m2+sm3

    avg=expr $ tot/3

    echo name is:$ name

    echo no is : $ no

    echo avg is: $ avg

    echo total is: $ tot

    count=expr $ count+1

    OUTPUT :

    enter the name: student

    enter the no:1

    enter the marks m1,m2,m3:100 100 100

    total is:300

    avg is:100

  • 8/8/2019 OS Programs for Record

    13/71

    SYSTEM CALLS USING FORK()

    PROGRAM :

    #include#include#include#includemain(){int pid;int a,b,t;

    printf("\nEnter a and b :");scanf("%d%d",&a,&b);

    pid=fork();if(pid

  • 8/8/2019 OS Programs for Record

    14/71

    printf("\nParent process");wait(2);}

    printf("pid value as %d\n",getpid());printf("A as:%d,B as:%d\n",a,b);}

    OUTPUT :

    Enter a and b : 4

    5

    Child process to do swapping

    pid value as 6651

    A as:5,B as:4

    Parent process pid value as 6649

    A as:4,B as:5

  • 8/8/2019 OS Programs for Record

    15/71

    SYSTEM CALLS USING EXEC()

    PROGRAM :

    #include#include#include#includemain(int age ,char *args[]){int pid;pid=fork();

    if(pid

  • 8/8/2019 OS Programs for Record

    16/71

    }

    OUTPUT :

    Child Process

    Child Completed

  • 8/8/2019 OS Programs for Record

    17/71

    FILE CREATION

    PROGRAM :

    #include#include#include#include#includemain(){int fd;char buf1[100],buf2[]="hello everybody",fname[30];

    printf("enter filename\n");scanf("%s",fname);fd = creat(fname,S_IRWXU);if(fd < 0){

    printf("error in file creation");}else{write(fd,buf2,30);

    printf("contents written successfully\n");

    fd=open(fname,O_RDONLY);read(fd,buf1,30);

    printf("contents is:\n%s\n",buf1);close(fd);

  • 8/8/2019 OS Programs for Record

    18/71

    }}

    OUTPUT :

    jec@jec-desktop:~/uma$ cc systemcall3.c

    jec@jec-desktop:~/uma$ ./a.out

    enter filename

    devi

    contents written successfully

    contents is:

    hello everybody

    jec@jec-desktop:~/uma$ cat devi

    hello everybody

  • 8/8/2019 OS Programs for Record

    19/71

    FILE ATTRIBUTES

    PROGRAM:

    #include

    #include

    #include

    #include

    int main()

    {

    struct stat statv;

    if(stat("/home/staff/rajkumar/ww",&statv)==-1)

    perror("stat");

    else

    {

    printf("\n the file user id is: %d",statv.st_uid);

    printf("\n the file group id is: %d",statv.st_gid);

    printf("\n the file size is :%d",statv.st_size);

    printf("\n the file inode number is :%d",statv.st_ino);

    }

    return (0);

    }

  • 8/8/2019 OS Programs for Record

    20/71

    OUTPUT:

    the file user id is: 920

    the file group id is: 921

    the file size is :33

    the file inode number is :8230320

  • 8/8/2019 OS Programs for Record

    21/71

    CHANGE THE CONTEXT OF A PROCESS

    PROGRAM:

    #include

    #include

    #include

    #include

    #include

    #include

    int system (const char *cmd)

    {

    pid_t pid;

    int status;

    switch(pid=fork())

    {

    case -1:

    return -1;

    case 0:

    execl ("/bin/sh","sh","-c",cmd,0);

    perror ("execl");

    exit(errno);

    }

    if(waitpid(pid,&status,0)==pid && WIFEXITED(status))

    return WEXITSTATUS(status);

    return -1;

  • 8/8/2019 OS Programs for Record

    22/71

    }

    int main()

    {

    int rc=0;

    char buf[256];

    do

    {

    printf("\n sh>");

    if(!gets(buf))

    break;

    rc=system(buf);

    }

    while(!rc);

    exit(rc);

    }

  • 8/8/2019 OS Programs for Record

    23/71

    OUTPUT:

    sh>ls

    a.out Desktop gg m3.txt odd.sh p2.c prim.sh sal.sh small.sh

    bb fact.sh ha menu.sh p1o.c p2c.c raj simcd.c user.sh

    big.sh ff hai mm.c p1pc.c p3.c rajkumar simls.c ww

    chat1.c ff.sh io4.c mul.sh p1w.c p4.c rrr simmv.c

    chat2.c fib.sh m2.txt mylist p1z.c pn.sh rr.sh small1.sh

    sh>cal January 2009

    Su Mo Tu We Th Fr Sa

    1 2 3 4 5 6 7 8 9 10

    11 12 13 14 15 16 17

    18 19 20 21 22 23 24

    25 26 27 28 29 30 31

  • 8/8/2019 OS Programs for Record

    24/71

    SCANNING A DIRECTORY

    PROGRAM:

    #include

    #include

    //#include

    #include

    #include

    #include

    #if defined(BSD)&&I_POSIX_SOURCE

    #include

    typedef struct dirent Dirent;

    #else

    #include

    typedef struct dirent Dirent;

    #endif

    int main(int argc, char *argv[])

    {

    Dirent* dp;

    int i;

    int cnt;

    DIR *dir_fdesc;

    while(--argc>0)

    {

    dir_fdesc=opendir(*++argv);

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    25/71

    if(i)

    printf("%s\n",dp->d_name);

    //if(strcmp(dp->d_name,".")&&strcmp(dp->d_name,".."))

    cnt++;

    }

    if(!cnt)

    {

    rmdir(*argv);

    break;

    }

    rewinddir(dir_fdesc);

    }

    closedir(dir_fdesc);

    }

    }

  • 8/8/2019 OS Programs for Record

    26/71

    OUTPUT:

    /a.out /home/staff/rajkumar/ggpp.sh

    hh.txt

    rajkumar

    gg

  • 8/8/2019 OS Programs for Record

    27/71

    IMPLEMENTATION OF I/O SYSTEM CALLS

    PROGRAM:

    #include

    #include

    #include

    int main(int argc, char *argv[])

    {

    char buf[256];

    int fdesc, len;

    while(--argc>0)

    {

    if(access(*++argv,F_OK)){

    fdesc=open(*argv,O_WRONLY|O_CREAT,0744);

    write(fdesc, "HELLO WORLD\n",12);

    }

    else

    {

    fdesc=open(*argv,O_RDONLY);while(len=read(fdesc,buf,256))

    write(1,buf,len);

    }

    close(fdesc);

    }

    }

    OUTPUT:

  • 8/8/2019 OS Programs for Record

    28/71

    ./a.out ram.c

    [cse@localhost ~]$ vi ram.c

    HELLO WORLD

    SIMULATION OF UNIX COMMANDSPROGRAM :

    #include

  • 8/8/2019 OS Programs for Record

    29/71

    int main(){char c;int n;

    printf("1.List of files in the directory\n2.List the lines\n3.Copy\n4.Remove\n");scanf("%d",&n);switch(n){case 1:system("ls");

    break;case 2:system("grep argc john.c");

    break;case 3:system("cp z c");

    printf("the file is copied");

    break;case 4:system("rm z c");

    printf("the file is deleted");break;}}

    OUTPUT :

    1.List of files in the directory

    2.List the lines

    3.Copy

    4.Remove

    1

    Ric.c

  • 8/8/2019 OS Programs for Record

    30/71

    Sab.c

    Sat.c

    1.List of files in the directory

    2.List the lines

    3.Copy

    4.Remove

    2

    int main(int argc,char * argv[])

    1.List of files in the directory

    2.List the lines

    3.Copy

    4.Remove

    3

    The file is copied

    1.List of files in the directory

    2.List the lines

    3.Copy

    4.Remove

    4

    The file is deleted

    FIRST COME FIRST SERVE SCHEDULING

    PROGRAM :

    #include

    int main()

    {

    int n,i,b[10],w[10],t[10];

    float ae,aw,at;

    printf("\n\tFIRST COME FIRST SERVE");

    printf("\n\t-------------------------------------");

    printf("\n\nENTER THE NUMBER OF JOBS : ");

  • 8/8/2019 OS Programs for Record

    31/71

    scanf("%d",&n);

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    32/71

    ENTER THE BURST TIME FOR THE JOB3 : 30

    ENTER THE BURST TIME FOR THE JOB4 : 40

    JOB BURSTTIME WAITING TIME TURNAROUNDTIME

    -------------------------------------------------------------------------------------------------

    1 10 0 10

    2 20 10 30

    3 30 30 60

    4 40 60 100

    AVERAGE BURSTTIME IS = 25.0000000

    AVERAGE WAITING TIME IS = 24.9999992

    AVERAGE TURNAROUNDTIME IS = 50.00000

    SHORTEST JOB FIRST SCHEDULING

    PROGRAM :

    #include

    int main()

    {

    int n,b[12],b1[12],j[12],w[12],e[12];

    int i,k,t,t1;

    float aw=0,ae=0;

    printf("\n\tSHORTEST JOB FIRST\n");

    printf("\t-----------------------------------");

    printf("\n\nENTER THE NUMBER OF JOBS : ");

  • 8/8/2019 OS Programs for Record

    33/71

    scanf("%d",&n);

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    34/71

    OUTPUT :

    SHORTEST JOB FIRST

    -------------------------------

    ENTER THE NUMBER OF JOBS: 3

    ENTER THE BURST TIME FOR THE JOB1 = 30

    ENTER THE BURST TIME FOR THE JOB2 = 10

    ENTER THE BURST TIME FOR THE JOB3 = 20

    JOB BURSTTIME WAITING TIME TURNAROUNDTIME

  • 8/8/2019 OS Programs for Record

    35/71

    -----------------------------------------------------------------------------------------------

    1 10 0 10

    2 20 10 30

    3 30 30 60

    AVERAGE WAITING TIME IS = 13.333333

    AVERAGE TURNAROUNDTIME IS = 33.333332

    PRIORITY SCHEDULING

    PROGRAM :

    #include

    int main(){

    int i,j,n,temp,start[10],pri[10],finish[10],burst[10],sw=0,st=0;

    printf("\nPRIORITY SCHEDULING\n");

    printf("\nENTER THE NUMBER OF JOBS : ");

    scanf("%d",&n);

    start[0]=0;

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    36/71

    for(j=i+1;j

  • 8/8/2019 OS Programs for Record

    37/71

    4 2 6 10

    8 3 10 18

    AVERAGE TURNAROUND TIME IS 11.333333

    AVERAGE WAITING TIME IS 5.333333

    ROUND ROBIN SCHEDULING

    PROGRAM :

    #include

    main()

    {

    int i, wa=0,cum[30],b[30],count=0,f=0,wait[30],b1[30];

    int k,x[30],xx[30],a,t=0,w,c,n,max=0,wt=0,s,j;printf("\nROUND ROBIN SCHEDULING");

    printf("\nENTER THE NUMBER OF JOBS : ");

    scanf("%d",&n);

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    38/71

    printf("\nENTER THE TIME SLICE FOR JOBS : ");

    scanf("%d",&s);

    k=1;

    for(j=0;j

  • 8/8/2019 OS Programs for Record

    39/71

    }

    }

    }

    printf("\n WAITING TIME");

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    40/71

    2 4 6 8 10 12

    WAITING TIME

    4

    2

    6

    AVERAGE WAITING TIME IS 4.000000

    AVERAGE EXECUTION TIME IS 8.00000

    AVERAGE TURN AROUND TIME IS 12.000000

    CHATTING PROGRAM

    PROGRAM:

    #include

    #include

    #include

    #include

    #include

    #include

    int main()

    {

    int mid;

    char mess[20];

    printf("\n\n INTER PROCESS COMMUNICATION USING MESSAGE QUEUE...");

    mid=msgget(27,IPC_CREAT|0777);

    if(mid==-1)

    {

    printf("\n\t invalid message id..");

  • 8/8/2019 OS Programs for Record

    41/71

    exit(1);

    }

    printf("\n enter the message text:");

    scanf("%s",mess);

    msgsnd(mid,mess,strlen(mess),0);

    printf("\n\n message has been sent..\n");

    printf("\n the sent message is:%s\n",mess);

    }

    OUTPUT:

    INTER PROCESS COMMUNICATION USING MESSAGE QUEUE...

    enter the message text:haimessage has been sent..

    the sent message is:hai

  • 8/8/2019 OS Programs for Record

    42/71

    PROGRAM:

    #include

    #include

    #include#include

    #include

    #include

    int main()

    {

    int i,mid,len;

    char mess[20];

    printf("\n\n INTER PROCESS COMMUNICATION USING MESSAGE QUEUE...");

    mid==msgget(27,IPC_CREAT|0777);

    if(mid==-1)

    {

    printf("\n\t invalid message id..");

    exit(1);

    }

  • 8/8/2019 OS Programs for Record

    43/71

    msgrcv(mid,mess,100,0,0);

    printf("\n\n message has been recieved..\n");

    printf("\n\n the recieved message is:%s\n",mess);

    }

    OUTPUT:

    INTER PROCESS COMMUNICATION USING MESSAGE QUEUE...

    message has been recieved..

    the recieved message is:hai

  • 8/8/2019 OS Programs for Record

    44/71

    PRODUCER CONSUMER PROBLEM

    PROGRAM :

    #include

    #include

    #include

    #define bz 10

    void producer();

    void consumer(int y[bz]);

    int wait(int);

    int signal(int);

    int i,n,in=0,out=0,s,c,w,sig,mutex=1,np[bz],buffer[bz],ne[bz];

    main()

    {

    do

    {

    printf("\n 1.produce the items\n2.consumer the items\n");

    scanf("%d",&s);

    if(s==1)

    {

    producer();

    }

  • 8/8/2019 OS Programs for Record

    45/71

    if(s==2)

    {

    consumer(buffer);

    }

    printf("\nto continue ,press 1 or else press 0");

    scanf("%d",&c);

    }

    while(c!=0);

    }

    void producer()

    {

    w=wait(mutex);

    printf("\nthe semaphore value of wait is =%d\n",w);

    printf("\nEnter the no of items:\n");

    scanf("%d",&n);

    printf("\nEnter the items");

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    46/71

    {

    buffer[i]=np[i];

    printf("%d\n",buffer[in]);

    in=(in+1)%bz;

    }

    sig=signal(mutex);

    printf("the semaphore value of signal is %d\n",sig);

    }

    void consumer(int y[bz])

    {

    if(mutex)

    printf("\n the semsphore value of wait is=%d\n",w);

    if(in==out)

    {

    printf("\n do nothing\n");

    }

    printf("\nthe consumer consumes the items:\n");

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    47/71

    int wait(int n)

    {

    n++;

    return(n);

    }

    OUTPUT :

    1.produce the items

    2.consumer the items

    1

    The semaphore value of wait is =2

    Enter the no of items:

    3

    Enter the items2

    3

    4

    1

    given items are:2

    3

    4

    1

    the producer produce the items:

    2

    3

    4

    1

    the semaphore value of signal is 2

  • 8/8/2019 OS Programs for Record

    48/71

    to continue ,press 1 or else press 01

    1.produce the items

    2.consumer the items

    2

    the semsphore value of wait is=2

    the consumer consumes the items:

    2

    3

    4

    1

    the semaphore value of signal is 2

    to continue ,press 1 or else press 0

  • 8/8/2019 OS Programs for Record

    49/71

    DINING PHILOSOPHERS PROBLEM

    PROGRAM :

    #includechar state[10],self[10],spoon[10];void test(int k){

    if((state[(k+4)%5]!='e')&&(state[(k+1)]!='e')){state[k]='e';self[k]='s';spoon[k]='n';spoon[(k+4)%5]='n';}

    }void pickup(int i){state[i]='h';

    test(i);if(state[i]=='h')self[i]='w';

    }void putdown(int i){state[i]='t';spoon[i]='s';spoon[i-1]='s';test((i+4)%5);test((i+1)%5);

    }main(){int ch,a,n,i;printf("\nDINING PHILOSOPHERS PROBLEM :");for(i=0;i

  • 8/8/2019 OS Programs for Record

    50/71

    printf("\n%d\t\t%c\t\t%c\t\t%c",i+1,state[i],self[i],spoon[i]);

    printf("\n1.EXIT\n2.HUNGRY\n3.THINKING");

    printf("\nEnter your choice :");

    scanf("%d",&ch);

    while(ch!=1)

    {

    switch(ch)

    {

    case 1:

    return 0;

    case 2:

    printf("\nEnter which philosopher is hungry :");

    scanf("%d",&n);

    pickup(n-1);

    break;

    case 3:

    printf("\nEnter which philosopher is thinking :");

    scanf("%d",&n);

    putdown(n-1);

    break;

    }

    printf("State of each philosopher :");

    printf("\nPHIL NO:\tTHINK/EAT\tSTATUS\t\tSPOON");

    for(i=0;i

  • 8/8/2019 OS Programs for Record

    51/71

    }

    OUTPUT :

    DINING PHILOSOPHERS PROBLEM :

    Initial state of each philosopher :PHIL NO: THINK STATUS SPOON1 t s s2 t s s3 t s s4 t s s5 t s s

    1.EXIT

    2.HUNGRY3.THINKING

    Enter your choice :2

    Enter which philosopher is hungry :2State of each philosopher :PHIL NO: THINK/EAT STATUS SPOON1 t s n2 e s n3 t s s

    4 t s s5 t s s

    1.EXIT2.HUNGRY3.THINKING

    Enter your choice :3

    Enter which philosopher is thinking :4State of each philosopher :

    PHIL NO: THINK/EAT STATUS SPOON1 t s n2 e s n3 t s s4 t s n5 e s n

    1.EXIT

    2.HUNGRY

    3.THINKING

    Enter your choice :1

  • 8/8/2019 OS Programs for Record

    52/71

    PAGING

    PROGRAM :

    #include

    main(){

    int i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;

    printf("Memory management -Paging\n");

    printf("\n*****************************");

    printf("\nEnter size of physical memory");

    scanf("%d",&phymem);

    for(i=20,j=0;i

  • 8/8/2019 OS Programs for Record

    53/71

    OUTPUT :

    Memory management -Paging

    *****************************

    Enter size of physical memory

    64

    Enter size of frame or page

    8

    No of frames available are 8

    Enter page table

    1

    2

    3

    4

    5

    6

    7

    8

    Enter page no

    5

    Enter offset

    3

    The physical address is :31

  • 8/8/2019 OS Programs for Record

    54/71

    BEST FIT

    PROGRAM :

    #include

    main()

    {

    int i,n,m,ps[100],bs[150],j,pi[50],bi[50],t;

    printf("enter the no of blocks\n");

    scanf("%d",&n);

    printf("enter the block size\n");

    for(i=1;i

  • 8/8/2019 OS Programs for Record

    55/71

    }

    for(i=1;i

  • 8/8/2019 OS Programs for Record

    56/71

    225033004400enter no of process3enter process 1 size:225enter process 2 size:275enter process 3 size:325

    process 0 is allocated to block of size 200process 1 is allocated to block of size 250process 2 is allocated to block of size 300

    FIRST FIT

    PROGRAM :

    #include

    main()

    {

    int i,n,m,ps[100],bs[150],j;

  • 8/8/2019 OS Programs for Record

    57/71

    printf("enter the no of blocks\n");

    scanf("%d",&n);

    printf("enter the block size\n");

    for(i=1;i

  • 8/8/2019 OS Programs for Record

    58/71

    400

    2

    500

    3

    300

    4

    600

    enter no of process

    4

    enter process 1 size:

    200

    enter process 2 size:

    250

    enter process 3 size:

    350

    enter process 4 size:

    550

    process 1 is allocated to block of size 400

    process 2 is allocated to block of size 500

    process 3 is allocated to block of size 600

    process 4 is not allowed

    ORPHAN PROCESS

    PROGRAM:

    main()

    {

    if(fork()==0)

    {

  • 8/8/2019 OS Programs for Record

    59/71

    system("clear");

    printf("CHILD:i am child & my number is :%d \n",getpid());

    printf("CHILD: my parent is :%d \n",getppid());

    }

    else

    {

    printf("PARENT:i am parent:%d \n",getpid());

    printf("PARENT: my parent is :%d \n",getppid());

    }

    }

    OUTPUT:

    CHILD:i am child & my number is :2663

    CHILD: my parent is :1

  • 8/8/2019 OS Programs for Record

    60/71

    PARENT-CHILD PROCESS

    PROGRAM:

    main()

    {

    if(fork()==0)

    {

    system("clear");

    printf("CHILD:i am child & my number is :%d \n",getpid());

    printf("CHILD: my parent is :%d \n",getppid());

    }

    else

  • 8/8/2019 OS Programs for Record

    61/71

    {

    printf("PARENT:i am parent:%d \n",getpid());

    sleep(2);

    printf("PARENT: my parent is :%d \n",getppid());

    }

    }

    OUTPUT:

    PARENT:i am parent:2682

    CHILD:i am child & my number is :2683

    CHILD: my parent is :2682

    PARENT: my parent is :2434

  • 8/8/2019 OS Programs for Record

    62/71

  • 8/8/2019 OS Programs for Record

    63/71

    {

    dip=fork();

    if(dip==0)

    {

    printf("2st Child Process Id is %d\n",getpid());

    printf("2st Child Process TERMINATING FROM THE MEMORY\n");

    }

    else

    {

    cpid=wait(0);

    printf("Child with pid:%d is died\n",cpid);

    cpid=wait(0);

    printf("Child with pid:%d is died\n",cpid);

    }

    }

    }

    OUTPUT:

    1st Child Process Id is 2699

    1st Child Process TERMINATING FROM THE MEMORY

    2st Child Process Id is 2700

    2st Child Process TERMINATING FROM THE MEMORY

    Child with pid:2699 is died

    Child with pid:2700 is died

  • 8/8/2019 OS Programs for Record

    64/71

    ZOMBIE PROCESS

    PROGRAM:

    main()

    {

    int pid=fork();

    system("clear");

    printf("my number is %d\n",getpid());

    if(pid>0)

    {

    printf("My parent number is %d\n",getpid());

    sleep(05);

    }

  • 8/8/2019 OS Programs for Record

    65/71

    }

    OUTPUT:

    my number is 2716

    My parent number is 2716

    my number is 2717

  • 8/8/2019 OS Programs for Record

    66/71

    SINGLY LINKED LIST

    PROGRAM:

    #include

    #include

    struct queue

    {

    int no;

    struct queue *next;

    }

    *p,*q,*temp,*head=NULL;

    int main()

    {

    int ch;

  • 8/8/2019 OS Programs for Record

    67/71

    void enqueue();

    void dequeue();

    void list();

    while(1)

    {

    printf("\n 1...Insert");

    printf("\n 2...Delete");

    printf("\n 3...Display");

    printf("\n 4...Quit");

    printf("Enter your choice:");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    enqueue();

    break;

    case 2:

    dequeue();

    break;

    case 3:

    list();

    break;

    default:

    exit(0);

    }

    }

    return 0;

    }

    void enqueue()

  • 8/8/2019 OS Programs for Record

    68/71

    {

    temp=(struct queue*) malloc(sizeof(struct queue));

    printf("\nEnter number to be inserted:");

    scanf("%d",&temp->no);

    if(head==NULL)

    {

    head=temp;

    temp->next=NULL;

    }

    else

    {

    q=head;

    while(q->next!=NULL)

    {

    q=q->next;

    }

    q->next=temp;

    temp->next=NULL;

    }

    printf("\n\nElement %d is pushed into queue",temp->no);

    }

    void dequeue()

    {

    p=head;

    if(head==NULL)

    {

    printf("\n\nQueue is empty");

    }

    else

  • 8/8/2019 OS Programs for Record

    69/71

    {

    head=head->next;

    printf("\n\nElement %d is dequeued from queue",p->no);

    free(p);

    }

    }

    void list()

    {

    if(head==NULL)

    {

    printf("\n\nQueue is empty");

    }

    else

    {

    p=head;

    printf("\n\nElement present in queue\n");

    while(p!=NULL)

    {

    printf("%d\t",p->no);

    p=p->next;

    }

    }

    }

  • 8/8/2019 OS Programs for Record

    70/71

    OUTPUT:

    1...Insert

    2...Delete

    3...Display

    4...Quit

    Enter your choice:1

    Enter number to be inserted:12

    Element 12 is pushed into queue

    1...Insert

    2...Delete

    3...Display

    4...Quit

    Enter your choice:1

    Enter number to be inserted:13

    Element 13 is pushed into queue

    1...Insert

    2...Delete

    3...Display

  • 8/8/2019 OS Programs for Record

    71/71

    4...Quit

    Enter your choice:1

    Enter number to be inserted:14

    Element 14 is pushed into queue

    1...Insert

    2...Delete

    3...Display

    4...Quit

    Enter your choice:2

    Element 12 is dequeued from queue

    1...Insert

    2...Delete

    3...Display

    4...Quit

    Enter your choice:3

    Element present in queue

    13 14

    1...Insert

    2...Delete

    3...Display

    4...

    Quit