System Calls OS record

Embed Size (px)

Citation preview

  • 8/7/2019 System Calls OS record

    1/25

    PROGRAM:

    /*STAT SYSTEM CALL*/

    #include#include#include#includemain(){

    char fname[30];struct stat buf;printf("\n enter filename with path");scanf("%s",fname);if(stat(fname,&buf)==0)

    {printf("\n filename is %s",fname);printf("\n the user id of the owner is %d",buf.st_uid);printf("\n the size of id is %d bytes",buf.st_size);printf("\n the file permission id %d",buf.st_mode);

    }else{

    perror("stat");exit(0);

    }

    }

  • 8/7/2019 System Calls OS record

    2/25

    OUTPUT:

    enter filename with path: test

    filename is testthe user id of the owner is 604the size of id is 7587 bytesthe file permission id 33277

  • 8/7/2019 System Calls OS record

    3/25

    PROGRAM:

    /* WAIT SYSTEM CALL*/

    #includemain(){

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

  • 8/7/2019 System Calls OS record

    4/25

    OUTPUT:

    a.out first.c second.c test

    c_pgms compile temp

    Child Complete

  • 8/7/2019 System Calls OS record

    5/25

    PROGRAM:

    /* FORK SYSTEM CALL*/

    #include#includemain(){

    pid_t pid;pid=fork();if(pid==0){

    printf("\n child process starts");printf("\n the pid of the child process is %d",getpid());printf("\n child process ends");

    }

    else if(pid>0){

    printf("\n parent process atarts");printf("\n the pid of the parent process is %d",getpid());printf("\n parent process ends\n");

    }else{

    perror("fork");}

    }

  • 8/7/2019 System Calls OS record

    6/25

    OUTPUT:

    child process starts

    the pid of the child process is 4524child process endsparent process startsthe pid of the parent process is 4523parent process ends

  • 8/7/2019 System Calls OS record

    7/25

    PROGRAM:

    /* I/O SYSTEM CALL FOR CREATE AND CLOSE*/

    #include#include#include#includemain(){

    char fname[30];int fd,filemode;printf("\n enter the file name to be created with the path ");scanf("%s",fname);if(fd=creat(fname)

  • 8/7/2019 System Calls OS record

    8/25

    OUTPUT:

    enter the file name to be created with the path: /home/cs4097/vig3

    enter the permission for the file 666

    file is successfully createdfilename mode/home/cs4097/vig3 666

    file is closed succesfully

  • 8/7/2019 System Calls OS record

    9/25

    PROGRAM:

    /*I/O SYSTEM CALL FOR READ*/

    #include#include#include#include#includemain(){

    char fn[30];int fd,i=0;int actualbytes, nbytes,pos;char buffer[1000];

    printf("\n enter filename with path");scanf("%s",fn);printf("\n opening the file %s \n",fn);if((fd=open(fn,O_RDWR|O_CREAT|O_RDONLY,0644))

  • 8/7/2019 System Calls OS record

    10/25

    }elseprintf("\n\n the size of the file is %d",pos);

    if(close(fd)

  • 8/7/2019 System Calls OS record

    11/25

    OUTPUT:

    enter filename with path /home/cs4097/myfile.c

    opening the file /home/cs4097/myfile.c

    enter maximum amount of data to be read5

    file is opened successfullyreading the first 5 bytes from the files

    the contents are

    #incl

    determining the size of file using lseek

    the size of the file is 735

    file is closed

  • 8/7/2019 System Calls OS record

    12/25

    PROGRAM:

    /*I/O SYSTEM CALL FOR WRITE*/

    #include#include#include#includemain(){

    char fn[30];int fd,i=0;int actualbytes,bytesstored;char buffer[100];printf("\nenter the filename with path");scanf("%s",fn);if((fd=open(fn,O_RDWR|O_CREAT|O_RDONLY,0611))

  • 8/7/2019 System Calls OS record

    13/25

    perror("close");exit(0);

    }else

    {printf("file closed");}

    }}

  • 8/7/2019 System Calls OS record

    14/25

    OUTPUT:

    enter the filename with path tamil

    enter the contents for the file press ctrl+d at the end of the file

    Hello tamil and Hello world

    file is opened successfullythe contents are writtenfile closed[root@linuxserver record]# cat tamil

    Hello tamil and Hello world

  • 8/7/2019 System Calls OS record

    15/25

  • 8/7/2019 System Calls OS record

    16/25

    OUTPUT:

    dshare2.c4038 new a.out .. shmget.cfile3hdhgdhgdhgdgdhfsxgf dir1 s .bash_logout vignesh mr.lesshstirectorypr.ch oustat.comnn.txt .kde jj write.c producerconsumer.c waite.c lsc .bashrc create.c madhuy .zshrc .viminfooi.c balaji m1.c fl2 ipc.c hex . ls.c f2 newfiletamil.c grep.c .bash_history fl1.c fl1 fs.c producer.c wait.c

  • 8/7/2019 System Calls OS record

    17/25

    PROGRAM:

    /*SIMULATION OF GREP COMMAND*/

    #include#includemain(){

    FILE *fp;char c[100],pat[10];int l,i,j=0,count=0,len,k,flag=0;printf("\n enter the pattern");scanf("%s",pat);len=strlen(pat);

    fp=fopen("tamil.c","r");while(!feof(fp)){

    fscanf(fp,"%s",c);l=strlen(c);count++;for(i=0;i

  • 8/7/2019 System Calls OS record

    18/25

    OUTPUT:

    enter the pattern sd

    the pattern sd present in word 3the pattern sd present in word 4

  • 8/7/2019 System Calls OS record

    19/25

    PROGRAM:

    /*PRODUCER CONSUMER PROBLEM(PCP)*/

    #include#include#include#include#include#include#include#include#include

    #define EMPTY -2#define TERMINATE -1

    int main(){

    sem_t* mutex;

    sem_unlink("/mutex_lab");

    int temp;

    mutex = (sem_t*)sem_open("/mutex_lab",O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO,1); //READ WRITE EXECUTE (RWX);

    int* buffer = mmap(0, sizeof(int)*50, PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);*buffer = EMPTY;

    int pid = fork();

    setbuf(stdout,0);

    if (pid != 0)while(1){sem_wait(mutex);

    temp = *buffer;

  • 8/7/2019 System Calls OS record

    20/25

    printf("\n\n\n");printf("\n(ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE

    PROGRAM\n");

    if(temp == EMPTY){printf("\nEnter the data to be produced!!!!!...\n");scanf("%d",&temp);

    if(temp==TERMINATE){

    printf("\nPROCESS(ES) TERMINATING NOW!!!!!!!....");kill(0,SIGTERM);exit(1);

    }

    printf("\nProduced...{ %d }\n",temp);*buffer = temp;}elseprintf("\n {FULL} BUFFER FULL!!!Producer is Waiting For the Consumer...\n\n");

    sem_post(mutex);

    if(rand()%2)sleep(1);}

    elsewhile(1){sem_wait(mutex);

    temp = *buffer;

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

    if(temp==EMPTY)printf("\n{ EMPTY } No data....consumer is waiting for Producer...\n");elseprintf("\nTHE DATA CONSUMED IS....%d. Now buff is {EMPTY}",temp);

  • 8/7/2019 System Calls OS record

    21/25

  • 8/7/2019 System Calls OS record

    22/25

    OUTPUT:

    [root@linuxserver record]$ cc pcs15.c lpthread[root@linuxserver record]$ ./a.out

    { EMPTY } No data....consumer is waiting for Producer...

    (ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAMEnter the data to be produced!!!!!...5Produced...{ 5 }

    THE DATA CONSUMED IS....5. Now buff is {EMPTY}

    { EMPTY } No data....consumer is waiting for Producer...

    (ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAMEnter the data to be produced!!!!!...3Produced...{ 3 }

    (ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAM{FULL} BUFFER FULL!!!Producer is Waiting For the Consumer...

    THE DATA CONSUMED IS....3. Now buff is {EMPTY}

    (ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAMEnter the data to be produced!!!!!...4Produced...{ 4 }

    THE DATA CONSUMED IS....4. Now buff is {EMPTY}

    (ENTER -1) IN THE PRODUCTION VALUE TO TERMINATE THE PROGRAMEnter the data to be produced!!!!!...-1PROCESS(ES) TERMINATING NOW!!!!!!!....Terminated

  • 8/7/2019 System Calls OS record

    23/25

    PROGRAM:

    /*IPC SHARED MEMORY1 (SENDER)*/

    #include#include#include#include#include#include #define K 1024int main(){

    int shmid;char *addr1;key_t key;key = ftok("/home/cs4038/share.c",'R');shmid = shmget(key,128*K,IPC_CREAT|SHM_R|SHM_W);addr1=shmat(shmid,0,0);printf("\n IPC using shared memory \n");printf("\n SENDER ADDRESS\n");printf("\n the address is %p",addr1);printf("\n enter the message:");scanf("%s",addr1);printf("\n MESSAGE STORED IN %p is %s",addr1,addr1);return 0;

    }

  • 8/7/2019 System Calls OS record

    24/25

    PROGRAM:

    /*IPC SHARED MEMORY2 (RECIEVER)*/

    #include#include#include#include#include#include

    #define K 1024main(){

    int shmid;char *addr1;key_t key;key = ftok("/home/cs4038/share.c",'R');shmid=shmget(key,128*K,SHM_R|SHM_W);addr1=shmat(shmid,0,0);printf("\n ipc using shared memory");printf("\n receiver address\n");printf("\n the address is %p",addr1);printf("\n message retrieved from %pi is %s",addr1,addr1);

    }

  • 8/7/2019 System Calls OS record

    25/25

    OUTPUT:

    [root@linuxserver record]$ cc share.c[root@linuxserver record] $./a.out

    IPC using shared memory

    SENDER ADDRESS

    the address is 0x2b48307c9000enter the message:Beautiful

    MESSAGE STORED IN 0x2b48307c9000 is Beautiful

    [root@linuxserver record]# cc share2.c

    [root@linuxserver record]# ./a.out

    ipc using shared memoryreceiver address

    the address is 0x2b88c8382000message retrieved from 0x2b88c8382000i is Beautiful