0% found this document useful (0 votes)
10 views7 pages

Tut Quiz 2023(Solved)

The document is a tutorial test for the Operating Systems course at Birla Institute of Technology and Science, Pilani, for the first semester of 2023-24. It includes programming questions related to file handling, process management, and inter-process communication using C language and system calls. The test consists of multiple questions requiring code implementations and theoretical answers, with a total duration of 50 minutes and a maximum score of 50 marks.

Uploaded by

Ram R
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)
10 views7 pages

Tut Quiz 2023(Solved)

The document is a tutorial test for the Operating Systems course at Birla Institute of Technology and Science, Pilani, for the first semester of 2023-24. It includes programming questions related to file handling, process management, and inter-process communication using C language and system calls. The test consists of multiple questions requiring code implementations and theoretical answers, with a total duration of 50 minutes and a maximum score of 50 marks.

Uploaded by

Ram R
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/ 7

Birla InstituteOf Technology and Science, Pilani

Departmentof Computer Scicnce and Information Systems


First Semester 2023-24
Tutorial Test (CLOSED BOOK)
22/11/2023
Subject: OPERATING SYSTEMS Time: 50 Mins
COURSE NO: CS F372 Max. Marks: 50

Q1. Ansvwer the following questions.

a) WriteCcode to print only file names from the current directory. The code should not print any of the
directories or ",* And |5M]
b) Write a C program to do the following using System Calls: |5M]
i. Make a directory named QUIZ".
ii. Create two directories inside "QUIZ" called *OS" and "CA".
i. Create two text files: "OS.txt" in folder "OS" and "CA.txt" in folder CA.
iv. Write BITS" to "OS.txt and "Pilani" to "CA.txt"
V. Append "CA.txt" to "OS.txt". (Output in "OS.txt" will be *Bits Pilani")
vi. Close the files and the directory.

Q2.
file system that holds 10
a) A process makes a request to access a byte at the offset of 7,00,000 in a logical
indirect block
direct, 3 single indirect, andldouble indirect blocks. The size of each block is 1K bytes. Each block. Show
accessed byte within the
can point to 256 blocks. Find out the block number and offset of the
(5M)
all computations. (Consider 1Kbytes=1024bytes)
with each block having a capacity
b) Ifa logical file system holds 8 direct,2 single indirect, and 1double indirect, following questions: [2M]
answer the
of 1K bVtes and Each indirect block can point to 256 blocks, then
single and double indirect
i. What are the maximum possible file sizes that can be addressed by the
blocks? (Answer in bytes or KB)
requests to
11 Find out the block number and offset of the accessed byte within the block ifa process
access bytesoffset 2,00,000.
Q3. the logic given below (system" function not
a) Write a Cprogram using pipe system calls to execute
allowed). |5M]
ps -e |grep abc
system calls.
b) Write the code toimplement the following scenarios using |10M]
i Create two process in a way that both process should use separate address space for their execution.
ii lmnlement communication between the above two processes. The first process issues a command to
transfer the file names and directory names present inside lhe current directory to second process using
directory system calls. The second process should recciveoutput
the information
device.
from the first process and print
the total count of those files and directories on standard

statements are T'rue or False,


Q4. Answer if the following |2M]
Fork)svstem callresults in the parent and child process sharing the same address space.
II. Vfork) sVstem call uses the Copy on Write(CoW) scheme for memory management.
Page 1 of 2
IIJ. Fork) system callsuspends execution of the parent process tillthe child completes its execution.
IV. Fork) system call results in unpredictable execution order of child and parent process.

O5. Implement Inter-ProcessCommunication between two processes "display'" and read' using FIFO. The code
should have the following functionality: |10M]
The display programcreates a FIFO for the initial communication between the display and read process
using the pathname "/tmp/myfifo".
ii. The display process sends a request to the read process through the first message over FIFO requesting
for a file's content. In response, The read process sends the input file name (read from the user) back to
the display process.
The display process, on receipt of the file name, prints it on the terminal. lt also sends a counter starting
with "1" back to the read process.
The read process responds to the counter message by sending back one line read from the input file. This
continues till the input file terminates.
On termination, the read process responds with "done" and this causes both the display and read processes
to exit.

Q6. Refer this code and answer the questions that follow: |4M)
void main(void) {
int i;
for(i=0;i<n;it+) {
fork();
if(fork()&&fork()X
printf("Pilani");
}
fork();
printf("%d\n"getpid();

a) Find how many times "Pilani" will be printed for n=2.


b) Rewrite the above code to redirect its standard output to a file "out.txt"

Q7. In a UNIX system with the directory structure as given below: [2M)

root code rootfile.txt

home abc code userfile.txt

Following system calls are executed:


link ("root/code/rootfile.txt ","home/abclcode/userfile.txt");
link ("/root, "home/abc ");
List out allthe possible ways to access userfile.txt

kk k

Page 2 of 2
OS Tutorial Test Solutions
Q1. a)
#include<stdio.h> 5
#include<dirent.h>
#include<string.h>
int main(int argc,char *argv[])
{
DIR *ds,*cld; struct dirent *dir;
ds = opendir(argv[1]);
if(ds == NULL)
printf("directory %s is not opened\n",argv[1]);
else
printf("Directory opened\n");
printf("list of files only\n");
char current[50];
getcwd(current,50);
//printf("%s\n",current);
while((dir = readdir(ds)) != NULL)
if(strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0)
{
char temp[50]="";
strcat(temp,current);strcat(temp,"/");strcat(temp,dir->d_name);
int testch=chdir(temp);
if (testch == -1)
{printf("%s\n",dir->d_name);}
}
if((cld = closedir(ds)) == 0)
printf("%s is successfully closed\n",argv[1]);
else
printf("%s is not successfully closed\n",argv[1]);
}
Q1. b)
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{ 0.5M
mkdir("QUIZ",0777);
mkdir("QUIZ/OS",0777); 1M
mkdir("QUIZ/CA",0777);
int fd1=open("QUIZ/OS/OS.txt",O_CREAT|O_RDWR); 1M
int fd2=open("QUIZ/CA/CA.txt",O_CREAT|O_RDWR);
char bits[4] = "BITS",pilani[6]="PILANI",buffer[20];
int ret;
ret = write(fd1, bits, sizeof(bits));
ret = write(fd2, pilani, sizeof(pilani)); 1M
lseek(fd2,0,SEEK_SET);
ret = read(fd2, buffer, 10);
ret = write(fd1, buffer, sizeof(buffer)); 1M
close(fd1);close(fd2); 0.5M
}
Q2
a) 5M
3rd single indirect->161st block->608byte
b)
i) max size: single=520KB or 532480bytes; double=66056KB or 67641344bytes 1M
ii) 1st single indirect->187th block->320 offset 1M
Q3. a)
#include <fcntl.h> // 5M
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h> //
//
#define INPUT_END 1
#define OUTPUT_END 0
//
int main(int argc, char* argv[]) //
{ //
pid_t pid1;
pid_t pid2; //
int fd[2]; //
//
pipe(fd);
pid1 = fork(); //
//
if(pid1==0) //
{
close(fd[INPUT_END]);
dup2(fd[OUTPUT_END], STDIN_FILENO);
close(fd[OUTPUT_END]);
execlp("grep", "grep", "abc",(char*) NULL); //
} //
else //
{ //
pid2=fork(); //
//
if(pid2==0) //
{
close(fd[OUTPUT_END]);
dup2(fd[INPUT_END], STDOUT_FILENO);
close(fd[INPUT_END]);
execlp("ps","ps","-e",(char*) NULL); //
} //
//
close(fd[OUTPUT_END]);
close(fd[INPUT_END]);
waitpid(-1, NULL, 0);
waitpid(-1, NULL, 0);

}
}
Q3. b)
#include <fcntl.h> //
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h> //
#define WRITE_END 1
#define READ_END 0
int main(int argc, char* argv[]) //
{ //
pid_t pid1;
int fd[2]; //
char buff[256];
pipe(fd);
pid1 = fork(); //
if(pid1==0) // 2M
{
close(fd[READ_END]);
DIR *ds,*cld; struct dirent *dir;
ds = opendir(".");
if(ds == NULL)
printf("directory %s is not opened\n",argv[1]); 2M
else
printf("Directory opened\n");
printf("list of files only\n");
while((dir = readdir(ds)) != NULL){
printf("sending %s\n",dir->d_name);
write(fd[WRITE_END],dir->d_name,sizeof(dir->d_name));
}
write(fd[WRITE_END],"done",5); 2M
close(fd[WRITE_END]);
} //
else // 1M
{ //
close(fd[WRITE_END]);
int ctr=0;
while(1)
{
read(fd[READ_END],buff,256);
if(strcmp(buff,"done")==0) break;
ctr++;
}
printf("No. of Files read %d\n",ctr);
close(fd[READ_END]);
} 2M
} 1M
Q4 All False. 0.5 Marks each 2M
Q5
// display process
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd;

// FIFO file path


char * myfifo = "/tmp/myfifo";

// Creating the named file(FIFO)


// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666); 2M

char arr1[80], arr2[80];


fd = open(myfifo, O_RDWR);
// initial message
strcpy(arr2,"INITIATE");
write(fd, arr2, strlen(arr2)+1); 1M
int ctr=1;
while (1)
{
// Read from FIFO
read(fd, arr1, 80); 1M
// Print the read message
printf("Reader Says: %s\n", arr1);
if(strcmp(arr1,”done”)==0) break; 1M
sprintf(arr2,"%d",ctr);
write(fd, arr2, strlen(arr2)+1);
ctr++;

}
return 0;
}
// reader process
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd,fd2;

// FIFO file path


char * myfifo = "/tmp/myfifo";

// Creating the named file(FIFO)


// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666);
char arr1[80], arr2[80],buffer[1],send_buffer[100],k=0;
fd = open(myfifo, O_RDWR);
read(fd, arr1, sizeof(arr1));
printf("Display : %s\n", arr1);
fgets(arr2, 80, stdin); 1M
fd2=open(arr2, O_RDONLY);
write(fd, arr2, sizeof(arr2));
do
{
int ret = read(fd2, buffer, 1);
if(buffer[0]!='\n')
{
send_buffer[k++]=buffer[0];
} 2M
else if(buffer[0]!=EOF)
{
send_buffer[k]='\0';
write(fd, send_buffer, k++); 1M
read(fd, arr1, sizeof(arr1));
printf("Display Count: %s\n", arr1);
k=0;
}
}while(buffer[0]!=EOF);
strcpy(arr2,"done"); 1M
write(fd, arr2, strlen(arr2)+1);
close(fd);
return 0;
}
Q6
a) 96 times 2M
b) int i; 2M
fd=open(“out.txt”, O_WRONLY | O_APPEND); //any one is OK
dup2(fd,STDOUT);//or 1 in place of STDOUT
for…
Q7
3 ways to access the file 2M
/root/code/rootfile.txt
/root/code/userfile.txt
/home/abc/code/userfile.txt

You might also like