Basic Programs of Operating System

Home  >>  Computer Science  >>  Basic Programs of Operating System

Basic Programs of Operating System

25
Nov,2016

0
  1. Program for creating a file and writing into it.

    #include<sys/types.h>
    #include<stdio.h>
    #include<fcntl.h>
    int main()
     {
    char buff[100];
    int fd1;
    int x=read(0,buff,30);
    fd1=open(“abc.txt”,O_CREAT|O_WRONLY,0777);
    int y=write(fd1,buff,x);

    }

  2. Program for reading a existing file.

    #include<sys/types.h>
    #include<stdio.h>
    #include<fcntl.h>
    int main()
    {
    char buff[100];
    int fd1;
    fd1=open(“abc.txt”,O_RDONLY);
    int x=read(fd1,buff,30);
    write(fd1,buff,x);

    }

  3. Program to read from a file and write into the other file.

    #include<sys/types.h>
    #include<stdio.h>
    #include<fcntl.h>
    int main()
    {
    char buff[100];
    int fd1,fd2;
    fd1=open(“abc.txt”,O_RDONLY);
    fd2=open(“xyz.txt”,O_WRONLY);
    int x=read(fd1,buff,30);
    write(fd2,buff,x);

    }

  4. Program to set pointer to read from 5th character.

    #include<sys/types.h>
    #include<stdio.h>
    #include<fcntl.h>
    int main()
    {
    char buff[100];
    int fd1;
    fd1=open(“f1”,O_RDONLY);
    int x=lseek(fd1,4,SEEK_SET);
    int y=read(fd1,buff,6);
    write(1,buff,y);
    printf(“%d”,x);

    }

  5. Program to create process(Parent Child relationship).

    #include<sys/types.h>
    #include<stdio.h>
    int main()
    {
    write(1,”Hello”,5);
    fork();    //create child process
    write(1,”Zenith”,2);
    }
    Output: HelloZenithZenith

  6. Program to create process with pid_t.

    #include<sys/types.h>
    #include<stdio.h>
    int main()
    {
    pid_t pid;            //data type for process id
    pid=fork();          //returns the process id
    if(pid<0)
    {
    printf(“ERROR”);
    }
    else if(pid==0)
    {
    printf(“i am child having id %d”,getpid());
    printf(“My parent id is %d”,getppid());
    }
    else
    {
    printf(“i am parent having id %d”,getpid());
    printf(“My child id is %d”,pid());
    }
    }

  7. Program to print 1 to 10 in parent process and 20 to 30 in child process.

    #include<sys/types.h>
    #include<stdio.h>
    int main()
    {
    int i;
    pid_t pid;            //data type for process id
    pid=fork();          //returns the process id
    if(pid<0)
    {
    printf(“ERROR”);
    }
    else if(pid==0)
    {
    for(i=20;i<=3=;i++)
    {
    printf(“%d”,i);
    sleep(1);   //pause for 1 min
    }
    }
    else
    {
    wait();         //child process will execute first then parent
    for(i=1;i<=10;i++)
    {
    printf(“%d”,i);
    sleep(1);
    }
    }
    }

  8. Race Condition.

    #include<pthread.h>
    int count;
    void *f1(void *)
    {
    int x;
    x=count;
    x++;
    count=x;
    }
    void *f2(void *)
    {
    int y;
    y=count;
    y++;
    count=y;
    }
    main()
    {
    pthread_t t1,t2;
    pthread_create(&t1,NULL,f1,NULL);
    pthread_create(&t2,NULL,f2,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf(“value of count variable %d”,count);
    }

    Output: value of count variable 1.

  9. Context Switch.

    #include<pthread.h>
    int count;
    void *f1(void *a)
    {
    int x;
    x=count;
    x++;
    sleep(1);
    count=x;
    }
    void *f2(void *b)
    {
    int y;
    y=count;
    y–;
    sleep(1);
    count=y;
    }
    main()
    {
    pthread_t t1,t2;
    pthread_create(&t1,NULL,f1,NULL);
    pthread_create(&t2,NULL,f2,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf(“value of count variable %d”,count);
    }

    Output: value of count variable 0 or 2.

    For avoiding Race Condition, Mutex Lock and Semaphore are used.

  10. Mutex Lock.

    #include<pthread.h>
    #include<stdio.h>
    int count=1;
    pthread_mutex_t l;
    void *f1(void *a)
    {
    pthread_mutex_lock(&l);
    int x;
    x=count;
    x++;
    sleep(1);
    count=x;
    pthread_mutex_unlock(&l);
    }
    void *f2(void *b)
    {
    pthread_mutex_lock(&l);
    int y;
    y=count;
    y–;
    sleep(1);
    count=y;
    pthread_mutex_unlock(&l);
    }
    main()
    {
    pthread_mutex_init(&l,NULL);
    pthread_t t1,t2;
    pthread_create(&t1,NULL,f1,NULL);
    pthread_create(&t2,NULL,f2,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf(“value of count variable %d”,count);
    }

    Output: value of count variable 1.

  11. Semaphores.

    #include<pthread.h>
    #include<stdio.h>
    #include<semaphore.h>
    sem_t s;
    int count=1;
    void *f1(void *a)
    {
    sem_wait(&s);
    int x;
    x=count;
    x++;
    sleep(1);
    count=x;
    sem_post(&s);
    }
    void *f2(void *b)
    {
    sem_wait(&s);
    int y;
    y=count;
    y–;
    sleep(1);
    count=y;
    sem_post(&s);
    }
    main()
    {
    sem_init(&s,0,1);  //0 = no of processes sharing semaphore ,1 = initial value of semaphore
    pthread_t t1,t2;
    pthread_create(&t1,NULL,f1,NULL);
    pthread_create(&t2,NULL,f2,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf(“value of count variable %d”,count);
    }

    Output: value of count variable 1.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *