0% found this document useful (0 votes)
11 views9 pages

OS 4 and 8 PGM

Uploaded by

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

OS 4 and 8 PGM

Uploaded by

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

Lab 04) Develop a C program which demonstrates interprocess

communication between a reader process and a writer


process. Use mkfifo, open, read, write and close APIs in
your program.

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "myfifo"
int main()
{
int fd;
char buffer[BUFSIZ];

// Create the FIFO (named pipe) if it doesn't already exist


if (mkfifo(FIFO_NAME, 0666) == -1)
{
perror("mkfifo");
exit(EXIT_FAILURE);
}

// Writer process
if (fork() == 0)
{
printf("Writer process is running. Enter data to write (type 'exit' to quit):\n");

fd = open(FIFO_NAME, O_WRONLY);
if (fd == -1) {
perror("open"); exit(EXIT_FAILURE);
}

while (1) {
fgets(buffer, BUFSIZ, stdin);

if (strcmp(buffer, "exit\n") == 0) { break;


}

write(fd, buffer, strlen(buffer) + 1);

close(fd);
}
// Reader process
else {
printf("Reader process is running. Reading data from the FIFO:\n");

fd = open(FIFO_NAME, O_RDONLY);
if (fd == -1) {
perror("open"); exit(EXIT_FAILURE);
}

while (1) {
if (read(fd, buffer, BUFSIZ) == 0) { break;
}

printf("Received: %s", buffer);


}

close(fd);
}

return 0;
}
Output
Lab 08) Simulate following File Organization Techniques
a) Single level directory b) Two level directory

Single level directory: In a single level directory system, all the files are
placed in one directory. This is very common on single-user OS's. A single-
level directory has significant limitations, however, when the number of
files increases or when there is more than one user. Since all files are in
the same directory, they must have unique names. If there are two users
who call their data file "test", then the unique-name rule is violated.
Although file names are generally selected to reflect the content of the
file, they are often quite limited in length. Even with a single-user, as the
number of files increases, it becomes difficult to remember the names of
all the files in order to create only files with unique names shown in below
figure.

Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];

printf("Enter the directory name:");


scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another file(yes - 1 or no - 0):");
scanf("%d",&ch);
}while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
getch();
}

Input:
Enter the directory name:KLEIT
Enter the number of files:3
Enter file name to be created:CSE
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:ECE
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:MECH
Do you want to enter another file(yes - 1 or no - 0):0

Output:
Directory name is:KLEIT
Files names are:
CSE
ECE
MECH

Two level directory: In the two-level directory system, the system


maintains a master block that has one entry for each user. This master
block contains the addresses of the directory of the users. There are still
problems with twolevel directory structure. This structure effectively
isolates one user from another. This is an advantage when the users are
completely independent, but a disadvantage when the users want to
cooperate on some task and access files of other users. Some systems
simply do not allow local files to be accessed by other users shown in
below figure.
Code:
#include<stdio.h>
#include<conio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;

printf("enter number of directories:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)

printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}
getch();
}

Input
enter number of directories:3
enter directory 1 names:VTU
enter size of directories:3
enter subdirectory name and size:CSE 2
enter file name:SEM1
enter file name:SEM2
enter subdirectory name and size:MECH 3
enter file name:SEM3
enter file name:SEM4
enter file name:SEM5
enter subdirectory name and size:ECE 1
enter file name:SEM6
enter directory 2 names:KLEIT
enter size of directories:2
enter subdirectory name and size:CSDEPT 2
enter file name:ABC
enter file name:ABD
enter subdirectory name and size:MECHDEPT 1
enter file name:ABCD
enter directory 3 names:BVBCET
enter size of directories:1
enter subdirectory name and size:SCIENCE 3
enter file name:PHY
enter file name:CHE
enter file name:MAT

dirname size subdirname size files


***************************************************************************
***********
VTU 3 CSE 2 SEM1 SEM2
MECH 3 SEM3 SEM4 SEM5
ECE 1 SEM6

KLEIT 2 CSDEPT 2 ABC ABD


MECHDEPT 1 ABCD

BVBCET 1 SCIENCE 3 PHY CHE MAT

You might also like