Os Ankit Pokharel

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Programs using the following system calls of UNIX operating system fork,

Lab No:1
exec, getpid, exit, wait, close, stat, opendir, readdir
AIM:
To write C Programs using the following system calls of UNIX operating system fork,
exec, getpid, exit, wait, close, stat, opendir, readdir.

1. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEMS


(OPENDIR, READDIR, CLOSEDIR)

ALGORITHM:

STEP 1: Start the program.


STEP 2: Create struct dirent.
STEP 3: declare the variable buff and pointer dptr.
STEP 4: Get the directory name.
STEP 5: Open the directory.
STEP 6: Read the contents in directory and print it.
STEP 7: Close the directory.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

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


char buff[100];
struct dirent *dptr;
DIR *dirp;

printf("\n\n ENTER DIRECTORY NAME:


"); scanf("%s", buff);

if ((dirp = opendir(buff)) == NULL)


{ printf("The given directory does not exist\
n"); exit(1);
}

while ((dptr = readdir(dirp)) != NULL) {


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

1|Page
closedir(dirp);
return 0;
}

OUTPUT:

2| P a g e
2. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING
SYSTEM (fork, getpid, exit)
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2.
STEP 3: Call fork() system call to create process.
STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid().
STEP 6: Print the process id.
STEP 7:Stop the program
AIM:
This aims to demonstrate process creation using fork()
PROGRAM:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
int pid, pid1, pid2;
pid = fork();

if (pid == -1) {
printf("ERROR IN PROCESS CREATION\
n"); exit(1);
}

if (pid != 0) {
pid1 = getpid();
printf("\nThe parent process ID is %d\n",
pid1); } else {
pid2 = getpid();
printf("\nThe child process ID is %d\n", pid2);
}

return 0;
}

OUTPUT:

3|Page
Lab No:2 C programs to simulate UNIX commands like cp, ls, grep.

AIM:
To write C programs to simulate UNIX commands like cp, ls, grep.
This program aims to count the number of spaces in a file.

1. Program for simulation of cp unix commands

ALGORITHM:
STEP1: Start the program
STEP 2:Declare the variables ch, *fp, sc=0
STEP3: Open the file in read mode
STEP 4: Get the character
STEP 5: If ch== “ “ then increment sc value by one
STEP 6: Print no of spaces
STEP 7:Close the file

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

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


FILE *fp;
char ch;
int sc = 0;

if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}

fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Unable to open file %s\n", argv[1]);
return 1;
} else {
while ((ch = fgetc(fp)) != EOF) {
if (ch == ' ')
sc++;
}
printf("Number of spaces: %d\n", sc);

fclose(fp);
4|Page
}

return 0;

OUTPUT:

5|Page
2. PROGRAM FOR SIMULATION OF LS UNIX COMMANDS

ALGORTIHM:
STEP1 : Start the program
STEP2 : Open the directory with directory object dp
STEP3 : Read the directory content and print it.
STEP4: Close the directory.
AIM:
This program intends to list the contents of a directory specified by the user.
PROGRAM:
#include <stdio.h>
#include <dirent.h>
int main(int argc, char **argv) {
DIR *dp;
struct dirent *link;
if (argc != 2) {
printf("Usage: %s <directory>\n", argv[0]);
return 1;
}
dp = opendir(argv[1]);
if (dp == NULL) {
printf("Error opening directory: %s\n",
argv[1]); return 1;
}
printf("\nContents of the directory %s are:\n",
argv[1]); while ((link = readdir(dp)) != NULL) {
printf("%s\n", link->d_name);
}
closedir(dp);
return 0;
}
OUTPUT:

6|Page
3. PROGRAM FOR SIMULATION OF GREP UNIX COMMANDS

ALGORITHM STEP1:
Start the program
STEP2: Declare the variables fline[max], count=0, occurrences=0 and pointers
*fp, *newline.
STEP 3: Open the file in read mode.
STEP4: In while loop check fgets(fline,max,fp)!=NULL
STEP 5: Increment count value.
STEP 6: Check newline=strchr(fline, „\n‟)
STEP 7: print the count,fline value and increment the occurrence value.
STEP 8: Stop the program

AIM:
The aim of the provided program is to search for occurrences of a specific
word within a text file.

PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // Added for exit()
#define max 1024
void usage() {
printf("Usage: ./a.out filename word\n");
}

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


FILE *fp;
char fline[max];
char *newline;
int count = 0;
int occurrences = 0;

if (argc != 3) {
usage();
exit(1);
}

if (!(fp = fopen(argv[1], "r"))) {


printf("grep: could not open file: %s\n", argv[1]);
exit(1);
}
7|Page
while (fgets(fline, max, fp) != NULL) {
count++;
if ((newline = strchr(fline, '\n')) != NULL)
*newline = '\0';
if (strstr(fline, argv[2]) != NULL) {
printf("%s: %d %s\n", argv[1], count, fline);
occurrences++;
}
}

printf("Total occurrences of \"%s\" in \"%s\": %d\n", argv[2], argv[1], occurrences);


fclose(fp);
return 0;
}
OUTPUT:

8| Page
CPU SCHEDULING ALGORITHMS
Lab No:3
PRIORITY

1. AIM:

To write a C program for implementation of Priority scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and priority.
Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as turnaround time.
Step 8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pno;
int pri;
int pri;
int btime;
int wtime;
}sp;
int main()
{
int i,j,n;
int tbm=0,totwtime=0,totttime=0;
sp *p,t;
printf("\n PRIORITY SCHEDULING.\n");
printf("\n enter the no of process. ... \n");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("enter the burst time and priority:\n");
for(i=0;i<n;i++)
{
printf("process%d:”,i+1);
scanf("%d%d",&p[i].btime,&p[i].pri);
9| Page
p[i].pno=i+1;
p[i].wtime=0;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
printf("\n process\tbursttime\twaiting time\tturnaround time\n");
for(i=0;i<n;i++)
{
totwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pno,p[i].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].wtime+p[i].btime);
}
totttime=tbm+totwtime;
printf("\n total waiting time:%d",totwtime);
printf("\n average waiting time:%f",(float)totwtime/n);
printf("\n total turnaround time:%d",totttime); printf("\n
avg turnaround time:%f",(float)totttime/n);
}

OUTPUT:

10 | P a g e
CPU SCHEDULING ALGORITHMS
Lab No:3.
ROUND ROBIN SCHEDULING

Q2. AIM:

To write a C program for implementation of Round Robin scheduling algorithms.

ALGORITHM:

Step 1: Inside the structure declare the variables.


Step 2: Declare the variable i,j as integer, totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and priority and read the time quantum.
Step 5: Assign wtime as zero.
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as turnaround time.
Step 8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct rr
{
int pno,btime,sbtime,wtime,lst;
}p[10];
int main()
{
int pp=-1,ts,flag,count,ptm=0,i,n,twt=0,totttime=0;
printf("\n round robin scheduling ........... ");
printf("enter no of processes:");
scanf("%d",&n);
printf("enter the time slice:");
scanf("%d",&ts);
printf("enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process%d\t",i+1);
scanf("%d",&p[i].btime);
p[i].wtime=p[i].lst=0;
p[i].pno=i+1;
p[i].sbtime=p[i].btime;
}
11 | P a g e
printf("scheduling .... \n");
do
{
flag=0;
for(i=0;i<n;i++)
{
count=p[i].btime;
if(count>0)
{
flag=-1;
count=(count>=ts)?ts:count;
printf("\n process %d",p[i].pno);
printf("from%d",ptm);
ptm+=count;
printf("to%d",ptm);
p[i].btime-=count;
if(pp!=i)
{
pp=i;
p[i].wtime+=ptm-p[i].lst-count;
p[i].lst=ptm;
}
}

OUTPUT:

12 | P a g e
CPU SCHEDULING ALGORITHMS
Lab No:3
FCFS

Q3. AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.

ALGORITHM:

Step 1: Inside the structure declare the variables.


Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait
time and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number
of process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct fcfs
{
int pid;
int btime;
int wtime;
int ttime;
}
p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0;
printf("\n fcfs scheduling...\n");
printf("enter the no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i].pid=1;
printf("\n burst time of the process”);
scanf("%d",&p[i].btime);
}
13 | P a g e
p[0].wtime=0;
p[0].ttime=p[0].btime;
totttime+=p[i].ttime;
for(i=0;i<n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].btim
p[i].ttime=p[i].wtime+p[i].btime;
totttime+=p[i].ttime;
towtwtime+=p[i].wtime;
}
for(i=0;i<n;i++)
{{
printf("\n waiting time for process”);
printf("\n turn around time for process”);
printf("\n");
}}
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time :%f",(float)totwtime/n);
printf("\n total turn around time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}

OUTPUT:

14 | P a g e
28 | P a g e

You might also like