Tut Quiz 2023(Solved)
Tut Quiz 2023(Solved)
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
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();
Q7. In a UNIX system with the directory structure as given below: [2M)
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;
}
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;