0% found this document useful (0 votes)
70 views

OS Lab

The document outlines 15 experiments for a Unix/Linux programming lab, including writing C programs to copy files using standard I/O and system calls, emulate the ls -l command, illustrate concurrent execution of threads using pthreads, simulate CPU scheduling algorithms and memory management strategies, and solve classical synchronization problems like producer-consumer using semaphores. Students are also instructed to study core Unix commands and utilities, text editors, shells, and file systems as part of Experiment 1.

Uploaded by

kingbeats492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

OS Lab

The document outlines 15 experiments for a Unix/Linux programming lab, including writing C programs to copy files using standard I/O and system calls, emulate the ls -l command, illustrate concurrent execution of threads using pthreads, simulate CPU scheduling algorithms and memory management strategies, and solve classical synchronization problems like producer-consumer using semaphores. Students are also instructed to study core Unix commands and utilities, text editors, shells, and file systems as part of Experiment 1.

Uploaded by

kingbeats492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

R20 II-I CSE UNIX PROGRAMMING LAB

1) a) Study of Unix/Linux general purpose utility command list: man,who,cat, cd, cp, ps, ls,
mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal,
logout, shutdown.
b) Study of vi editor
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
d) Study of Unix/Linux file system (tree structure)
e) Study of .bashrc, /etc/bashrc and Environment variables.

2) Write a C program that makes a copy of a file using standard I/O, and system calls.

3) Write a C program to emulate the UNIX ls –l command.

4) Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort.

5) Simulate the following CPU scheduling algorithms:


(a) Round Robin (b) SJF (c) FCFS (d) Priority

6) Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and


exit (), System calls.

7) Simulate the following:


a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)

8) Simulate Bankers Algorithm for Dead Lock Avoidance.

9) Simulate Bankers Algorithm for Dead Lock Prevention.

10) Simulate the following page replacement algorithms:


a) FIFO b) LRU c) LFU

11) Simulate the following File allocation strategies


(a) Sequenced (b) Indexed (c) Linked
12) Write a C program that illustrates two processes communicating using shared memory.

13) Write a C program to simulate producer and consumer problem using semaphores.

14) Write C program to create a thread using pthreads library and let it run its function.

15) Write a C program to illustrate concurrent execution of threads using pthreads library.
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT - 1

1) a) Study of Unix/Linux general purpose utility command list: man,who,cat, cd, cp, ps, ls,
mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal,
logout, shutdown.
b) Study of vi editor
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
d) Study of Unix/Linux file system (tree structure)
e) Study of .bashrc, /etc/bashrc and Environment variables.

MAN Command:

man is the system's manual viewer; it can be used to display manual pages, scroll up
and down, search for occurrences of specific text, and other useful functions.
Syntax:

$ man cat

WHO Command:

who command displays information about the current status of system.


who options file

Who as default prints login names of users currently logged in.


Options

• -a use all options.


• -b Report information about last reboot.
• -d report expired processes.
• -H print headings.
• -p report previously spawned processes.
• -u report terminal usage.

Cat Command

cat filename
cat is used to display the contents of the file.

cat is used to create file


cat> filename

Cd Command.
R20 II-I CSE UNIX PROGRAMMING LAB

Cd directoryname

Will change directory from current directory to specified directory.

Cal command

cal command will print the calendar on current month by default. If you want to print
calendar of august of 1965. That's eighth month of 1965.

cal 8 1965 will print following results.

August 1965
S M Tu W Th F S
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

Pwd command.

pwd command will print your home directory on screen, pwd means print working
directory.
/home/satish

Ls command

ls command is most widely used command and it displays the contents of directory.

options

• ls will list all the files in your home directory, this command has many options.
R20 II-I CSE UNIX PROGRAMMING LAB

• ls -l will list all the file names, permissions, group, etc in long format.

• ls -a will list all the files including hidden files that start with . .

• ls -lt will list all files names based on the time of creation, newer files bring first.

• ls -Fxwill list files and directory names will be followed by slash.

• ls -Rwill lists all the files and files in the all the directories, recursively.

• ls -R | more will list all the files and files in all the directories, one page at a time.

Mkdir command

mkdir aditya
will create new directory, i.e. here aditya directory is created.

Cd command

cd aditya
will change directory from current directory to aditya directory. Use pwd to check your
current directory and ls to see if aditya directory is there or not.

Chmod command

chmod command is used to change permissions on a file.

cal.txt.

initially when this file will be created the permissions for this file depends upon umask set
in your profile files. As you can see this file has

666 or -rw-rw-rw attributes.


ls -la cal.txt
-rw-rw-rw- 1 ssb dxidev 135 Dec 3 16:14 cal.txt
In this line above I have -rw-rw-rw- meaning respectively that owner can read and
write file, member of the owner's group can read and write this file and anyone else
connected to this system can read and write this file., next ssb is owner of this file dxidev is
the group of this file, there are 135 bytes in this file, this file was created on December 3 at
R20 II-I CSE UNIX PROGRAMMING LAB

time16:14 and at the end there is name of this file. Learn to read these permissions in
binary, like this for example Decimal 644 which is 110 100 100 in binary meand rw-r--r--
or user can read,write this file, group can read only, everyone else can read only. Similarly,
if permissions are 755 or 111 101 101 that means rwxr-xr-x or user can read, write and
execute, group can read and execute, everyone else can read and execute. All directories
have d in front of permissions. So if you don't want anyone to see your files or to do
anything with it use chmod command and make permissions so that only you can read and
write to that file, i.e.
chmod 600 filename.

mkdir Command

mkdir dirname Makes a new directory

rm Command
The rm command removes (deletes) files or directories.
rm filename Deletes filename

rmdir Command

rmdir dirname Removes directory

Date command.
Date displays todays date, to use it type date at prompt.
Sun Dec 7 14:23:08 EST 1997
is similar to what you should see on screen.

Time Command time - time a simple command or give resource usage

The time command runs the specified program command with the given arguments.
When command finishes, time writes a message to standard error giving timing statistics
about this program run.
R20 II-I CSE UNIX PROGRAMMING LAB

PS command
ps command is probably the most useful command for systems administrators. It reports
information on active processes.
ps options

Options.

-a Lists all processes in system except processes not attached to terminals.


-e Lists all processes in system.
-f Lists a full listing.
-j print process group ID and session ID.

MV Command

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

mv change file name or directory location


mv filename1 filename2

cp Command cp - copy files and directories

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

cp [OPTION]... [-T] SOURCE DEST

Kill Command

The command kill sends the specified signal to the specified process or process group. If
no signal is specified, the TERM signal is sent.

kill - terminate a process


ECHO Command
- display a line of text

More Command
Viewing file contents page by page.
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT- 2

Aim : Write a C program that makes a copy of a file using standard I/O, and system
calls

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#define BUF_SIZE 8192

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

int input_fd, output_fd; /* Input and output file descriptors */


ssize_t ret_in, ret_out; /* Number of bytes returned by read() and write() */
char buffer[BUF_SIZE]; /* Character buffer */

/* Are src and dest file name arguments missing */

if(argc != 3){
printf ("Usage: cp file1 file2");
return 1;
}

/* Create input file descriptor */


input_fd = open (argv [1], O_RDONLY);
if (input_fd == -1) {
perror ("open");
return 2;
}

/* Create output file descriptor */


output_fd = open(argv[2], O_WRONLY | O_CREAT, 0644);
if(output_fd == -1){
perror("open");
return 3;
}

/* Copy process */
while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){
R20 II-I CSE UNIX PROGRAMMING LAB

ret_out = write (output_fd, &buffer, (ssize_t) ret_in);


if(ret_out != ret_in){
/* Write error */
perror("write");
return 4;
}
}

/* Close file descriptors */


close (input_fd);
close (output_fd);

return (EXIT_SUCCESS);
}

Output : user@ITDS07:~$ cc Prog1.c


user@ITDS07:~$ ./a.out source_file.txt destination_file.txt
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT- 3

Write a C program to emulate the UNIX ls –l command.


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process
if ( pid < 0 )
{ //fail
printf(“\nFork failed\n”);
exit (-1);
}
else if ( pid == 0 )
{ //child
execlp ( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”);
exit (0);
}
}
Output:

guest-glcbIs@ubuntu:~$gcc –o lsc.out lsc.c


guest-glcbIs@ubuntu:~$./lsc.out
total 100
-rwxrwx—x 1 guest-glcbls guest-glcbls 140 2012-07-06 14:55 f1
drwxrwxr-x 4 guest-glcbls guest-glcbls 140 2012-07-06 14:40 dir1
child complete
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT- 4
Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("pipe failed");
exit(1);
}
if(!fork())
{
close(1);
dup(pfds[1];
system (“ls –l”);
}
else
{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}
Output:

[student@gcet ~]$ vi pipes2.c


[student@gcet ~]$ cc pipes2.c
[student@gcet ~]$ ./a.out
Parent reading from pipe
Total 24
-rwxrwxr-x l student student 5563Aug 3 10:39 a.out
-rw-rw-r—l
Student student 340 jul 27 10:45 pipe2.c
-rw-rw-r—l student student
Pipes2.c
-rw-rw-r—l student student 401 34127 10:27 pipe2.c
student
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT- 5
Simulate the following CPU scheduling algorithms:
(a) Round Robin (b) SJF (c) FCFS (d) Priority

(a) Round Robin

#include<stdio.h>

#include<conio.h>

void main()

int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;

int bt[10],flag[10],ttat=0,twt=0;

float awt,atat;

clrscr();

printf("\t\t ROUND ROBIN SCHEDULING \n");

printf("Enter the number of Processors \n");

scanf("%d",&n);

n1=n;

printf("\n Enter the Timeslice \n");

scanf("%d",&ts);

for(i=1;i<=n;i++)

printf("\n Enter the process ID %d",i);

scanf("%d",&pid[i]);

printf("\n Enter the Burst Time for the process");

scanf("%d",&bt[i]);

need[i]=bt[i];
R20 II-I CSE UNIX PROGRAMMING LAB

for(i=1;i<=n;i++)

flag[i]=1;

wt[i]=0;

while(n!=0)

for(i=1;i<=n;i++)

if(need[i]>=ts)

for(j=1;j<=n;j++)

if((i!=j)&&(flag[i]==1)&&(need[j]!=0))

wt[j]+=ts;

need[i]-=ts;

if(need[i]==0)

flag[i]=0;

n--;

else

for(j=1;j<=n;j++)
R20 II-I CSE UNIX PROGRAMMING LAB

if((i!=j)&&(flag[i]==1)&&(need[j]!=0))

wt[j]+=need[i];

need[i]=0;

n--;

flag[i]=0;

for(i=1;i<=n1;i++)

tat[i]=wt[i]+bt[i];

twt=twt+wt[i];

ttat=ttat+tat[i];

awt=(float)twt/n1;

atat=(float)ttat/n1;

printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");

printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime \n ");

for(i=1;i<=n1;i++)

printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);

printf("\n The average Waiting Time=%f",awt);

printf("\n The average Turn around Time=%f",atat);


R20 II-I CSE UNIX PROGRAMMING LAB

getch();

OUTPUT:

Enter the number of Processors

Enter the Timeslice

Enter the process ID 1 5

Enter the Burst Time for the process 10

Enter the process ID 2 6

Enter the Burst Time for the process 15

Enter the process ID 3 7

Enter the Burst Time for the process 20

Enter the process ID 4 8

Enter the Burst Time for the process 25

ROUND ROBIN SCHEDULING ALGORITHM

Process Process ID BurstTime Waiting Time TurnaroundTime

1 5 10 15 25

2 6 15 25 40

3 7 20 25 45

4 8 25 20 45

The average Waiting Time=4.2f

The average Turn around Time=4.2f


R20 II-I CSE UNIX PROGRAMMING LAB

b) Shortest Job First :

#include<stdio.h>

void main()

int i,j,k,n,sum,wt[10],tt[10],twt,ttat;

int t[10],p[10];

float awt,atat;

clrscr();

printf("Enter number of process\n");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter the Burst Time of Process %d",i);

scanf("\n %d",&t[i]);

for(i=0;i<n;i++)

p[i]=i;

for(i=0;i<n;i++)

for(k=i+1;k<n;k++)

if(t[i]>t[k])

int temp;
R20 II-I CSE UNIX PROGRAMMING LAB

temp=t[i];

t[i]=t[k];

t[k]=temp;

temp=p[i];

p[i]=p[k];

p[k]=temp;

printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");

printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUND TIME


\n\n");

wt[0]=0;

for(i=0;i<n;i++)

sum=0;

for(k=0;k<i;k++)

wt[i]=sum+t[k];

sum=wt[i];

for(i=0;i<n;i++)

tt[i]=t[i]+wt[i];

for(i=0;i<n;i++)
R20 II-I CSE UNIX PROGRAMMING LAB

printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);

twt=0;

ttat=t[0];

for(i=1;i<n;i++)

twt=twt+wt[i];

ttat=ttat+tt[i];

awt=(float)twt/n;

atat=(float)ttat/n;

printf("\n AVERAGE WAITING TIME %4.2f",awt);

printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);

getch();

OUTPUT:

Enter number of process

Enter the Burst Time of Process 04

Enter the Burst Time of Process 13

Enter the Burst Time of Process 25

SHORTEST JOB FIRST SCHEDULING ALGORITHM

PROCESS ID BURST TIME WAITING TIME TURNAROUND TIME

1 3 0 3
R20 II-I CSE UNIX PROGRAMMING LAB

0 4 3 7

2 5 7 12

AVERAGE WAITING TIME 3.33

AVERAGE TURN AROUND TIME 7.33


R20 II-I CSE UNIX PROGRAMMING LAB

c) FCFS Scheduling :

#include<stdio.h>

void main()

int i,n,sum,wt,tat,twt,ttat;

int t[10];

float awt,atat;

clrscr();

printf("Enter number of processors:\n");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter the Burst Time of the process %d",i+1);

scanf("\n %d",&t[i]);

printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");

printf("\n Process ID \t Waiting Time \t Turn Around Time \n");

printf("1 \t\t 0 \t\t %d \n",t[0]);

sum=0;

twt=0;

ttat=t[0];

for(i=1;i<n;i++)

sum+=t[i-1];

wt=sum;

tat=sum+t[i];

twt=twt+wt;
R20 II-I CSE UNIX PROGRAMMING LAB

ttat=ttat+tat;

printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);

printf("\n\n");

awt=(float)twt/n;

atat=(float)ttat/n;

printf("\n Average Waiting Time %4.2f",awt);

printf("\n Average Turnaround Time %4.2f",atat);

getch();

OUTPUT:

Enter number of processors:

Enter the Burst Time of the process 1: 2

Enter the Burst Time of the process 2: 5

Enter the Burst Time of the process 3: 4

FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Time Turn Around Time

1 0 2

2 2 7

3 7 11

Average Waiting Time 3.00

Average Turnaround Time 6.67


R20 II-I CSE UNIX PROGRAMMING LAB

d) Priority Scheduling:

#include <stdio.h>

#include <conio.h>

void main()

int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;

float awt,atat;

clrscr();

printf("\n-----------PRIORITY SCHEDULING------------- \n");

printf("Enter the No of Process: ");

scanf("%d", &n);

for (i=0;i<n;i++)

pid[i] = i;

printf("Enter the Burst time of Pid %d : ",i);

scanf("%d",&bt[i]);

printf("Enter the Priority of Pid %d : ",i);

scanf ("%d",&pr[i]);

// Sorting start

for (i=0;i<n;i++)

for(j=i+1;j<n;j++)

if (pr[i] > pr[j] )

t = pr[i];
R20 II-I CSE UNIX PROGRAMMING LAB

pr[i] = pr[j];

pr[j] = t;

t = bt[i];

bt[i] = bt[j];

bt[j] = t;

t = pid[i];

pid[i] = pid[j];

pid[j] = t;

tat[0] = bt[0];

wt[0] = 0;

for (i=1;i<n;i++)

wt[i] = wt[i-1] + bt[i-1];

tat[i] = wt[i] + bt[i];

printf("\n \n");

printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");

printf("\n \n");

for(i=0;i<n;i++)

printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);

for(i=0;i<n;i++)

ttat = ttat+tat[i];
R20 II-I CSE UNIX PROGRAMMING LAB

twt = twt + wt[i];

awt = (float)twt / n;

atat = (float)ttat / n;

printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);

getch();

OUTPUT:

Enter the No of Process: 4

Enter the Burst time of Pid 0 : 2

Enter the Priority of Pid 0 : 3

Enter the Burst time of Pid 1 : 6

Enter the Priority of Pid 1 : 2

Enter the Burst time of Pid 2 : 4

Enter the Priority of Pid 2 : 1

Enter the Burst time of Pid 3 : 5

Enter the Priority of Pid 3 : 7

Pid Priority Burst time WaitingTime TurnArroundTime

2 1 4 0 4

1 2 6 4 10

0 3 2 10 12

3 7 5 12 17

Avg.Waiting Time: 6.500000

Avg.Turn Around Time: 10.750000


R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-6
Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and
exit (), System calls.

#include<stdio.h>
void main()
{
int pid1,pid2,pid3;
printf("Parent id is %d and root id is%d\n",getpid(),getppid());
pid1=fork();
if(pid1==0)
{
printf("Process 1 id is %d and its parent id is %d\n",getpid(),getppid());
pid2=fork()
;
}
if(pid2==0)
{
printf("Process 2 id is %d and its parent id is %d\n",getpid(),getppid());
pid3=fork();
}
if(pid1==0&&pid2==0&&pid3==0)
{
printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid());
}
}

Output

$ cc process1.c$
a.out
Parent id is 3553 and root id is 2495
Process 1 id is 3554 and its parent id is 3553

Process 2 id is 3555 and its parent id is 3554


Process 3 id is 3556 and its parent id is 3555

EXEC1( ) :

#include<stdio.h>
#include<sys/types.h>
include<unistd.h>
void main()
{
R20 II-I CSE UNIX PROGRAMMING LAB

int pid1;
pid1=fork();
if(pid1==0)
{
printf("Process id is %d ",getpid());
printf("and its parent id is %d”,getppid());
execl("/bin/who","who",0);
}
}

Output

$ cc program2.c
$ a.out

Process id is 3553 and parent id is 2495

Root ttyp2 jun25 03.30

sit ttyp1 jun25 03.30


R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-7
Simulate the following:
a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)

a) Multiprogramming with a fixed number of tasks (MFT)

Program:

#include<stdio.h>

#include<conio.h>

main()

int i,m,n,tot,s[20];

clrscr();

printf("Enter total memory size:");

scanf("%d",&tot);

printf("Enter no. of pages:");

scanf("%d",&n);

printf("Enter memory for OS:");

scanf("%d",&m);

for(i=0;i<n;i++)

printf("Enter size of page%d:",i+1);

scanf("%d",&s[i]);

tot=tot-m;

for(i=0;i<n;i++)

if(tot>=s[i])
R20 II-I CSE UNIX PROGRAMMING LAB

printf("Allocate page %d\n",i+1);

tot=tot-s[i];

else

printf("process p%d is blocked\n",i+1);

printf("External Fragmentation is=%d",tot);

getch();

}
R20 II-I CSE UNIX PROGRAMMING LAB

OUTPUT:

Enter total memory size : 50

Enter no.of pages 4

Enter memory for OS 10

Enter size of page 10

Enter size of page 9

Enter size of page 9

Enter size of page 10

External Fragmentation is = 2
R20 II-I CSE UNIX PROGRAMMING LAB

b) Multiprogramming with a variable number of tasks (MVT)

Program:

#include<stdio.h>

#include<conio.h>

main()

int ms,i,ps[20],n,size,p[20],s,intr=0;

clrscr();

printf("Enter size of memory:");

scanf("%d",&ms);

printf("Enter memory for OS:");

scanf("%d",&s);

ms-=s;

printf("Enter no.of partitions to be divided:");

scanf("%d",&n);

size=ms/n;

for(i=0;i<n;i++)

printf("Enter process and process size");

scanf("%d%d",&p[i],&ps[i]);

if(ps[i]<=size)

intr=intr+size-ps[i];

printf("process%d is allocated\n",p[i]);

else
R20 II-I CSE UNIX PROGRAMMING LAB

printf("process%d is blocked",p[i]);

printf("total fragmentation is %d",intr);

getch();

OUTPUT:

Enter total memory size : 50

Enter memory for OS :10

Enter no.of partitions to be divided:4

Enter size of page : 10

Enter size of page : 9

Enter size of page : 9

Enter size of page : 8

Internal Fragmentation is = 4
R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-8
Simulate Bankers Algorithm for Dead Lock Avoidance
#include<stdio.h>

#include<conio.h>

void main()

int n,r,i,j,k,p,u=0,s=0,m;

int block[10],run[10],active[10],newreq[10];

int max[10][10],resalloc[10][10],resreq[10][10];

int totalloc[10],totext[10],simalloc[10];

//clrscr();

printf("Enter the no of processes:");

scanf("%d",&n);

printf("Enter the no ofresource classes:");

scanf("%d",&r);

printf("Enter the total existed resource in each class:");

for(k=1; k<=r; k++)

scanf("%d",&totext[k]);

printf("Enter the allocated resources:");

for(i=1; i<=n; i++)

for(k=1; k<=r; k++)

scanf("%d",&resalloc);

printf("Enter the process making the new request:");

scanf("%d",&p);

printf("Enter the requested resource:");

for(k=1; k<=r; k++)

scanf("%d",&newreq[k]);

printf("Enter the process which are n blocked or running:");

for(i=1; i<=n; i++)


R20 II-I CSE UNIX PROGRAMMING LAB

if(i!=p)

printf("process %d:\n",i+1);

scanf("%d%d",&block[i],&run[i]);

block[p]=0;

run[p]=0;

for(k=1; k<=r; k++)

j=0;

for(i=1; i<=n; i++)

totalloc[k]=j+resalloc[i][k];

j=totalloc[k];

for(i=1; i<=n; i++)

if(block[i]==1||run[i]==1)

active[i]=1;

else

active[i]=0;

for(k=1; k<=r; k++)

resalloc[p][k]+=newreq[k];

totalloc[k]+=newreq[k];
R20 II-I CSE UNIX PROGRAMMING LAB

for(k=1; k<=r; k++)

if(totext[k]-totalloc[k]<0)

u=1;

break;

if(u==0)

for(k=1; k<=r; k++)

simalloc[k]=totalloc[k];

for(s=1; s<=n; s++)

for(i=1; i<=n; i++)

if(active[i]==1)

j=0;

for(k=1; k<=r; k++)

if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))

j=1;

break;

if(j==0)
R20 II-I CSE UNIX PROGRAMMING LAB

active[i]=0;

for(k=1; k<=r; k++)

simalloc[k]=resalloc[i][k];

m=0;

for(k=1; k<=r; k++)

resreq[p][k]=newreq[k];

printf("Deadlock willn't occur");

else

for(k=1; k<=r; k++)

resalloc[p][k]=newreq[k];

totalloc[k]=newreq[k];

printf("Deadlock will occur");

getch();

OUTPUT:
Enter the
no of
processes
:4 Enter
the no
ofresourc
e
classes:3
Enter the total existed
resource in each class:3 2 2
Enter the allocated
resources:1 0 0 5 1 1 2 1 1 0
0 2Enter the process making
the new request:2
Enter the requested resource:1 1 2
Enter the process which are n
blocked or running:process 2:1 2
process 4:
1 0
process 5:
1 0
R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-9
Simulate Bankers Algorithm for Dead Lock Prevention.

#include<stdio.h>
#include<conio.h>
void main()
{
int
allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],
work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
clrscr();

printf("\n Enter number of process:");


scanf("%d",&pno);
printf("\n Enter number of resources:");
scanf("%d",&rno);
for(i=1;i< =pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);

printf("\n Enter Max resources for each process:");


for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}

printf("\n Enter allocated resources for each process:");


for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);

printf("\n available resources:\n");


for(j=1;j<= rno;j++)
{
avail[j]=0;
R20 II-I CSE UNIX PROGRAMMING LAB

total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}

do
{

for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}

printf("\n Allocated matrix Max need");


for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}

prc=0;

for(i=1;i<= pno;i++)
{
R20 II-I CSE UNIX PROGRAMMING LAB

if(flag[i]==0)
{
prc=i;

for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}

if(prc!=0)
break;
}

if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}

}while(count!=pno&&prc!=0);

if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");

getch();
}

OUTPUT

Enter number of process:5

Enter number of resources:3

Enter total numbers of each resources:10 5 7


R20 II-I CSE UNIX PROGRAMMING LAB

Enter Max resources for each process:


for process 1:7 5 3

for process 2:3 2 2

for process 3:9 0 2

for process 4:2 2 2

for process 5:4 3 3

Enter allocated resources for each process:


for process 1:0 1 0

for process 2:3 0 2

for process 3:3 0 2

for process 4:2 1 1

for process 5:0 0 2

available resources:
2 3 0

Allocated matrix Max need


0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
R20 II-I CSE UNIX PROGRAMMING LAB

Available matrix: 7 5 3
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 3 completed
Available matrix: 10 5 5
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-10
Simulate the following page replacement algorithms:
a) FIFO b) LRU c) LFU

a) FIFO

Program:

#include<stdio.h>

#include<conio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

void main()

clrscr();

printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");

printf("\n Enter no.of frames. .. ");

scanf("%d",&nof);

printf("Enter number of reference string..\n");

scanf("%d",&nor);

printf("\n Enter the reference string..");

for(i=0;i<nor;i++)

scanf("%d",&ref[i]);

printf("\nThe given reference string:");

for(i=0;i<nor;i++)

printf("%4d",ref[i]);

for(i=1;i<=nof;i++)

frm[i]=-1;

printf("\n");

for(i=0;i<nor;i++)

{
R20 II-I CSE UNIX PROGRAMMING LAB

flag=0;

printf("\n\t Reference np%d->\t",ref[i]);

for(j=0;j<nof;j++)

if(frm[j]==ref[i])

flag=1;

break;

}}

if(flag==0)

pf++;

victim++;

victim=victim%nof;

frm[victim]=ref[i];

for(j=0;j<nof;j++)

printf("%4d",frm[j]);

printf("\n\n\t\t No.of pages faults...%d",pf);

getch();

OUTPUT:

Enter no.of frames. .. 4

Enter number of reference string..

Enter the reference string..


R20 II-I CSE UNIX PROGRAMMING LAB

564123

The given reference string:

...................................... 5 6 4 1 2 3

Reference np5-> 5 -1 -1 -1

Reference np6-> 5 6 -1 -1

Reference np4-> 5 6 4 -1

Reference np1-> 5 6 4 1

Reference np2-> 2 6 4 1

Reference np3-> 2 3 4 1

No.of pages faults...6


R20 II-I CSE UNIX PROGRAMMING LAB

b) LRU

Program:

#include<stdio.h>

#include<conio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

int recent[10],lrucal[50],count=0;

int lruvictim();

void main()

clrscr();

printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");

printf("\n Enter no.of Frames. .. ");

scanf("%d",&nof);

printf(" Enter no.of reference string..");

scanf("%d",&nor);

printf("\n Enter reference string..");

for(i=0;i<nor;i++)

scanf("%d",&ref[i]);

printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");

printf("\n\t The given reference string:");

printf("\n…............................................. ");

for(i=0;i<nor;i++)

printf("%4d",ref[i]);

for(i=1;i<=nof;i++)

frm[i]=-1;
R20 II-I CSE UNIX PROGRAMMING LAB

lrucal[i]=0;

for(i=0;i<10;i++)

recent[i]=0;

printf("\n");

for(i=0;i<nor;i++)

flag=0;

printf("\n\t Reference NO %d->\t",ref[i]);

for(j=0;j<nof;j++)

if(frm[j]==ref[i])

flag=1;

break;

if(flag==0)

count++;

if(count<=nof)

victim++;

else

victim=lruvictim();

pf++;

frm[victim]=ref[i];
R20 II-I CSE UNIX PROGRAMMING LAB

for(j=0;j<nof;j++)

printf("%4d",frm[j]);

recent[ref[i]]=i;

printf("\n\n\t No.of page faults...%d",pf);

getch();

int lruvictim()

int i,j,temp1,temp2;

for(i=0;i<nof;i++)

temp1=frm[i];

lrucal[i]=recent[temp1];

temp2=lrucal[0];

for(j=1;j<nof;j++)

if(temp2>lrucal[j])

temp2=lrucal[j];

for(i=0;i<nof;i++)

if(ref[temp2]==frm[i])

return i;

return 0;

}
R20 II-I CSE UNIX PROGRAMMING LAB

OUTPUT:

Enter no.of Frames. .. 3

Enter no.of reference string 6

Enter reference string..

654231

The given reference string:

…………………. 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1

Reference NO 5-> 6 5 -1

Reference NO 4-> 6 5 4

Reference NO 2-> 2 5 4

Reference NO 3-> 2 3 4

Reference NO 1-> 2 3 1

No.of page faults...6


R20 II-I CSE UNIX PROGRAMMING LAB

c) Optimal

Program:

#include<stdio.h>

#include<conio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

int recent[10],optcal[50],count=0;

int optvictim();

void main()

clrscr();

printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");

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

printf("\nEnter the no.of frames");

scanf("%d",&nof);

printf("Enter the no.of reference string");

scanf("%d",&nor);

printf("Enter the reference string");

for(i=0;i<nor;i++)

scanf("%d",&ref[i]);

clrscr();

printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");

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

printf("\nThe given string");

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

for(i=0;i<nor;i++)

printf("%4d",ref[i]);

for(i=0;i<nof;i++)
R20 II-I CSE UNIX PROGRAMMING LAB

frm[i]=-1;

optcal[i]=0;

for(i=0;i<10;i++)

recent[i]=0;

printf("\n");

for(i=0;i<nor;i++)

flag=0;

printf("\n\tref no %d ->\t",ref[i]);

for(j=0;j<nof;j++)

if(frm[j]==ref[i])

flag=1;

break;

if(flag==0)

count++;

if(count<=nof)

victim++;

else

victim=optvictim(i);

pf++;
R20 II-I CSE UNIX PROGRAMMING LAB

frm[victim]=ref[i];

for(j=0;j<nof;j++)

printf("%4d",frm[j]);

printf("\n Number of page faults: %d",pf);

getch();

int optvictim(int index)

int i,j,temp,notfound;

for(i=0;i<nof;i++)

notfound=1;

for(j=index;j<nor;j++)

if(frm[i]==ref[j])

notfound=0;

optcal[i]=j;

break;

if(notfound==1)

return i;

temp=optcal[0];

for(i=1;i<nof;i++)

if(temp<optcal[i])
R20 II-I CSE UNIX PROGRAMMING LAB

temp=optcal[i];

for(i=0;i<nof;i++)

if(frm[temp]==frm[i])

return i;

return 0;

OUTPUT:

Enter no.of Frames. .. 3

Enter no.of reference string 6

Enter reference string..

654231

The given reference string:

…………………. 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1

Reference NO 5-> 6 5 -1

Reference NO 4-> 6 5 4

Reference NO 2-> 2 5 4

Reference NO 3-> 2 3 4

Reference NO 1-> 2 3 1

No.of page faults...6


R20 II-I CSE UNIX PROGRAMMING LAB

Experiment-11
Simulate the following File allocation strategies
(a) Sequenced (b) Indexed (c) Linked

(a) Sequenced

Program:

#include<stdio.h>

#include<conio.h>

main()

int n,i,j,b[20],sb[20],t[20],x,c[20][20];

clrscr();

printf("Enter no.of files:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter no. of blocks occupied by file%d",i+1);

scanf("%d",&b[i]);

printf("Enter the starting block of file%d",i+1);

scanf("%d",&sb[i]);

t[i]=sb[i];

for(j=0;j<b[i];j++)

c[i][j]=sb[i]++;

printf("Filename\tStart block\tlength\n");

for(i=0;i<n;i++)

printf("%d\t %d \t%d\n",i+1,t[i],b[i]);

printf("Enter file name:");


R20 II-I CSE UNIX PROGRAMMING LAB

scanf("%d",&x);

printf("File name is:%d",x);

printf("length is:%d",b[x-1]);

printf("blocks occupied:");

for(i=0;i<b[x-1];i++)

printf("%4d",c[x-1][i]);

getch();

OUTPUT:

Enter no.of files: 2

Enter no. of blocks occupied by file1 4

Enter the starting block of file1 2

Enter no. of blocks occupied by file2 10

Enter the starting block of file2 5

Filename Start block length

1 2 4

2 5 10

Enter file name: rajesh

File name is:12803 length is:0blocks occupied


R20 II-I CSE UNIX PROGRAMMING LAB

b) Indexed:

Program:

#include<stdio.h>

#include<conio.h>

main()

int n,m[20],i,j,sb[20],s[20],b[20][20],x;

clrscr();

printf("Enter no. of files:");

scanf("%d",&n);

for(i=0;i<n;i++)

{ printf("Enter starting block and size of file%d:",i+1);

scanf("%d%d",&sb[i],&s[i]);

printf("Enter blocks occupied by file%d:",i+1);

scanf("%d",&m[i]);

printf("enter blocks of file%d:",i+1);

for(j=0;j<m[i];j++)

scanf("%d",&b[i][j]);

} printf("\nFile\t index\tlength\n");

for(i=0;i<n;i++)

printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);

}printf("\nEnter file name:");


R20 II-I CSE UNIX PROGRAMMING LAB

scanf("%d",&x);

printf("file name is:%d\n",x);

i=x-1;

printf("Index is:%d",sb[i]);

printf("Block occupied are:");

for(j=0;j<m[i];j++)

printf("%3d",b[i][j]);

getch();

OUTPUT:

Enter no. of files:2

Enter starting block and size of file1: 2 5

Enter blocks occupied by file1:10

enter blocks of file1:3

2 5 4 6 7 2 6 4 7

Enter starting block and size of file2: 3 4

Enter blocks occupied by file2:5

enter blocks of file2: 2 3 4 5 6


File index length

1 2 10

2 3 5

Enter file name: venkat

file name is:12803


R20 II-I CSE UNIX PROGRAMMING LAB

c) Linked:

Program:

#include<stdio.h>

#include<conio.h>

struct file

char fname[10];

int start,size,block[10];

}f[10];

main()

int i,j,n;

clrscr();

printf("Enter no. of files:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter file name:");

scanf("%s",&f[i].fname);

printf("Enter starting block:");

scanf("%d",&f[i].start);

f[i].block[0]=f[i].start;

printf("Enter no.of blocks:");

scanf("%d",&f[i].size);

printf("Enter block numbers:");

for(j=1;j<=f[i].size;j++)
R20 II-I CSE UNIX PROGRAMMING LAB

scanf("%d",&f[i].block[j]);

printf("File\tstart\tsize\tblock\n");

for(i=0;i<n;i++)

printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);

for(j=1;j<=f[i].size-1;j++)

printf("%d--->",f[i].block[j]);

printf("%d",f[i].block[j]);

printf("\n");

getch();

OUTPUT:

Enter no. of files:2

Enter file name:venkat

Enter starting block:20

Enter no.of blocks:6

Enter block numbers: 4

12

15

45

32

25
R20 II-I CSE UNIX PROGRAMMING LAB

Enter file name:rajesh

Enter starting block:12

Enter no.of blocks:5

Enter block numbers:6

File start size block

venkat 20 6 4--->12--->15--->45--->32--->25

rajesh 12 5 6--->5--->4--->3--->2
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT – 12
Write a C program that illustrates two processes communicating using shared memory.

shm_server.c
-- simply creates the string and shared memory portion.
shm_client.c
-- attaches itself to the created shared memory portion and uses the string

shm_server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/*
* Now put some things into the memory for the
* other process to read.
*/
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
/*
* Finally, we wait until the other process
* changes the first character of our memory
R20 II-I CSE UNIX PROGRAMMING LAB

* to '*', indicating that it has read what


* we put there.
*/
while (*shm != '*')
sleep(1);

exit(0);
}

shm_client.c
/*
* shm-client - client program to demonstrate shared memory.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
/*
* We need to get the segment named
* "5678", created by the server.
*/
key = 5678;
/*
* Locate the segment.
*/
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}

/*
* Now read what the server put in the memory.
*/
for (s = shm; *s != NULL; s++)
R20 II-I CSE UNIX PROGRAMMING LAB

putchar(*s);
putchar('\n');

/*
* Finally, change the first character of the
* segment to '*', indicating we have read
* the segment.
*/
*shm = '*';
exit(0);
}
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT -13
Write a C program to simulate producer and consumer problem using semaphores.

PROGRAM:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n"); while(1) {
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n); switch(n) {
case 1:
if((mutex==1)&&(empty!=0))
producer();
else printf("BUFFER IS FULL"); break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else printf("BUFFER IS EMPTY"); break;
case 3: exit(0); break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer() {
mutex=wait(mutex);
full=signal(full);
empty=wait(empty); x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
R20 II-I CSE UNIX PROGRAMMING LAB

mutex=signal(mutex);
}

OUTPUT:
[root@localhost ~]# ./a.out 1.PRODUCER 2.CONSUMER 3.EXIT
ENTER YOUR CHOICE
1producer produces the item1
ENTER YOUR CHOICE
1producer produces the item2
ENTER YOUR CHOICE
2consumer consumes item2
ENTER YOUR CHOICE
2consumer consumes item1
ENTER YOUR CHOICE
2BUFFER IS EMPTY
ENTER YOUR CHOICE
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT – 14
Write C program to create a thread using pthreads library and let it run its function.

#
i
n
c
l
u
d
e

<
s
t
d
i
o
.
h
>

#
i
n
c
l
u
d
e
Compile:
<
s
• tC compiler: cc -lpthread pthread1.c
d
or
l
• iC++ compiler: g++ -lpthread pthread1.c
b
.
Run: ./a.out
h
>
Results:
Thread
# 1
Thread
i 2
Thread
n 1 returns: 0
Thread
c 2 returns: 0
l
u
d
e

<
p
t
h
r
e
a
d
.
h
>

void

*print_message_fu
R20 II-I CSE UNIX PROGRAMMING LAB

EXPERIMENT – 15
Write a C program to illustrate concurrent execution of threads using pthreads library.

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void *mythread1(void *vargp)


{
int i;
printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("exit from thread1\n");
return NULL;
}

void *mythread2(void *vargp)


{
int j;
printf("thread2 \n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);

printf("Exit from thread2\n");


return NULL;
}

int main()
{
pthread_t tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL);
exit(0);
}

OUT PUT ::

$ cc w8.c – l pthread

$./a.out
thread1
i=1
i=2;
i=3
thread2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
i=4
i=5
R20 II-I CSE UNIX PROGRAMMING LAB

i=6
i=7
i=8
i=9
i=10
exit from thread1
j=9
j=10
exit from thread2

You might also like