0% found this document useful (0 votes)
61 views44 pages

Operating Systems Programming Guide

Os file of govind

Uploaded by

bohararaj498
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)
61 views44 pages

Operating Systems Programming Guide

Os file of govind

Uploaded by

bohararaj498
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/ 44

COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.

)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

INDEX
S.NO. PROGRAM NAME DATE SIGNATURE REMARK
Write a Program to display a file pagewise
1. assuming a page has 10 lines and each line
has 80 characters
Write a Program which convert all the small
2. case letters in a file into appropriate capital
letters.
Write a program to print the details of the
3.
system (use uname sys call)
Write a program which will print the list of
4. environment variable and also print the value
of the PATH system variable
Write a programtoprint current(soft) limit
5.
and maximum (Hard) limits of all resources
Write a program with an exith and ler that
6.
outputs CPU usage.
Write a program that print sit’s & it sparent’s
7.
process ID.
Write a program that print sout various user
8.
& group ID’s.
Write a program which uses fork to create a
9. child process & then parent & child print their
respective process ID’s
Write a program that create sach a in
10. processes, where n is a command line
argument.
Write a program that creates a fan of n
11. processes where nis passed as a command
line argument.
Write a program to show that same opened
12. file can be shared by both parent and child
processes
Write a program that create child process to
13.
run ls – l
Write a program to create zombie child and
14.
find its status using system (ps) command
19.
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

15. Write a program to copy a file.

Write a program for which output is


16. automatically directed to a named file rather
than on to the console
Write a program that redirects standards
output to the file my.file(or Write a program
17.
that do the following operation cat XYZ > my
file). using dup 2 rather than dup
Write a program to create an empty directory
18.
using system calls
Write a program to remove a directory using
19.
system call
Write a program to output current working
20.
directory

21. Write a program to list files in a directory.


Write a program that returns true if a given
22.
file is a directory & false otherwise.
Write a program that can display the type of
23.
a given file like regular, directory etc.
Write a program to display the permission of
24.
a given file
Write a program to execute the equivalent of
25.
ls –l | sort –n +4
Write a program to handle SIGUSR1 and
26.
SIGUSR2 signal
Write a program which suspend sitse lf till it
27.
receives a SIGALARM signal
Write a program which prints the second’s
28. part of current time when ever the SIGALRM
signal is received by the program.
Write a Program to print the entries of
29.
passwd file for a given user name or userID
Write a program to print the details of the
30.
system
Write a program which Reads what Is written
31. To a named pipe & writes it to standard
output
Write a nin for mative message to a named
32.
pipe
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-1
OBJECTIVE – Write a Program to display a file page wise assuming a page has 10
lines and each line has 80 characters.
CODE–
#include<stdio.h>
#include<stdlib.h>

intmain(intargc,char*argv[]){ char
buff[10][80];
int i,j;char
k; FILE
*fp;
if(argc!=2)
{
fprintf(stderr,"Usage:./a.outfilename\n");
exit(1);
}
fp=fopen(argv[1],"r");
while(!feof(fp)){
for(i=0;i<10;i++)
for(j=0;j<80;j++)
buff[i][j]='\0';
for(i=0;i<10;i++)
fgets(buff[i],80,fp);
for(i=0;i<10;i++)
printf("%s",buff[i]);
scanf("%c",&k);
}
fclose(fp);
}
OUTPUT :-

Govind@Govind-Latitude-E5450: ~/os
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg1 pg1.c Govind@Govind-Latitude-E5450:~/0S$ ./pg1
pg1input.txt
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.
Line 11: This is the eleventh line.
Line 12: This is the twelfth line.
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-2

OBJECTIVE – Write a Program which converts all the small case letters in a file into appropriate
capital letters

CODE–

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

intmain(intargc,char*argv[]){
FILE *fp, *ft;
charch;

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

fp=fopen(argv[1],"r"); if
(fp == NULL) {
printf("Can'topenfile.\n");
exit(1);
}

ft=fopen("temp","w"); if
(ft == NULL) {
printf("Can'tcreatetemporaryfile.\n");
exit(1);
}

while((ch=fgetc(fp))!=EOF){ if
(ch >= 'a'&& ch <= 'z')
ch=ch-'a'+'A';
fputc(ch, ft);
}

fclose(fp);
fclose(ft);

fp=fopen(argv[1],"w"); if
(fp == NULL) {
printf("Can'topenfileforwriting.\n");
exit(1);
}
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

ft=fopen("temp","r"); if
(ft == NULL) {
printf("Can'topentemporaryfile.\n");
exit(1);
}

while((ch=fgetc(ft))!=EOF){
fputc(ch, fp);
}

fclose(fp);
fclose(ft);

printf("Conversionsuccessful.\n");

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/OS
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg2 pg2.c
Govind@Govind-Latitude-E5450:~/0S$ cat > pg2input.txt
my name is Govind
^C
Govind@Govind-Latitude-E5450:~/0S$ ./pg2 pg2input.txt
Conversion successful.
Govind@Govind-Latitude-E5450:~/0S$ cat pg2input.txt
MY NAME IS GOVIND
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-3

OBJECTIVE – Write a program to print the details of the system (use uname sys call)
CODE–

#include <stdio.h>
#include<sys/utsname.h>

intmain(){
structutsnameu;

if(uname(&u)!=0) {
fprintf(stderr,"UnameError\n");
return 1;
}

printf("SystemName:%s\n",u.sysname);
printf("NodeName:%s\n",u.nodename);
printf("Release: %s\n", u.release);
printf("Version: %s\n", u.version);
printf("Machine: %s\n", u.machine);

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg3.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg3 pg3.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg3
System Name: Linux
Node Name: Govind-Latitude-E5450
Release: 5.15.0-43-generic
Version: #46-Ubuntu SMP Tue Jul 12 10:30:17 UTC 2022
Machine: x86_64
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-4
OBJECTIVE – Write a program which will print the list of environment variable and also print the
value of the PATH system variable.

CODE–

//Toprintallenvironment variables

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

externchar **environ;
//theexternalvariableenvironpointstotheprocessenvironmentlistwhentheprocessbegins
executing. Do man environ

intmain(void)
{
inti;
char*path;
printf("Theenvironmentlistfollows:\n");
for(i=0;environ[i] != NULL; i++)
printf("environ[%d]:%s\n",i,environ[i]);

if((path=getenv("PATH"))==NULL)//domangetenv
printf("PATHenvironmentvariablenotset\n");
else
printf("ThevalueofPATHvariable=%s\n",path);
return0;
}

OUTPUT :-
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)
Govind@Govind-Latit
joеерaкgoеepak-Latitude-E5450:~/05 geall pg4.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg4 pg4.c
Govind@Govind-Latitude-E5450:~/0$ ./pg4
The environment list follows:
environ[0]: SHELL=/bin/bash
x
environ[1]: SESSION_MANAGER=local/Govind-Latitude-E5450:@/tmp/.ICE
-unix/1656,unix/Govind-Latitude-E5450:/tmp/.ICE-unix/1656
environ[2]: QT_ACCESSIBILITY=1 environ[3]: COLORTERM=truecolor
environ[4]: XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu: /etc/xdg environ[5]: SSH_AGENT_LAUNCHER-
gnome-keyring
environ[6]: XDG_MENU_PREFIX=gnome-
environ[7]: GNOME_DESKTOP_SESSION_ID=this-is-deprecated environ[8]: LANGUAGE=en_IN:en
environ[9]: GNOME_SHELL_SESSION_MODE=ubuntu
environ[10]: SSH_AUTH_SOCK=/run/user/1000/keyring/ssh environ[11]: XMODIFIERS=@im=ibus
environ[12]: DESKTOP_SESSION=ubuntu
environ[13]: GTK_MODULES-gail: atk-bridge
environ[14]: DBUS_STARTER_BUS_TYPE=session
environ[15]: PWD=/home/Govind/os
environ[16]: LOGNAME=Govind
environ[17]: XDG_SESSION_DESKTOP=ubuntu
environ[18]: XDG_SESSION_TYPE=wayland
environ[19]: SYSTEMD_EXEC_PID=1656
environ[20]: XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.QKEHM2 environ[21]:
HOME=/home/Govind
environ[22]: USERNAME=Govind
environ[23]: IM_CONFIG_PHASE=1 environ[24]: LANG=en_IN
environ[25]: LS_COLORS=rs=0: di=01;34:ln=01;36:mh=00:pi=40;33: so=01 ;35: do=01;35:bd=40; 33; 01:
cd=40; 33; 01: or=40; 31; 01:mi-00: su=37;41:sg =30;43: ca=30;41: tw=30; 42: ow=34;42:st=37;44: ex-
01;32:*.tar=01;31:*. tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha-01;31:*.lz4-01
;31:*.lzh=01;31:*.lzma-01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*
.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31
*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst-01;31:*.tzst=01;31:*.bz2=
L
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-5

OBJECTIVE – Write a program to print current (soft) limit and maximum (Hard) limits of all resources
CODE–

#include <stdio.h>
#include <stdlib.h>
#include<sys/resource.h>

int main() {
structrlimitrl;
int i;

printf("\nResourceName\tCurrentLimit\tMaxLimit\n");

for (i = 0; i <= RLIMIT_NLIMITS; i++) {


if(getrlimit(i,&rl)<0) {
printf("Erroringetrlimit\n");
exit(1);
}

switch(i){
caseRLIMIT_CPU:
printf("RLIMIT_CPU\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_DATA:
printf("RLIMIT_DATA\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_FSIZE:
printf("RLIMIT_FSIZE\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_MEMLOCK:
printf("RLIMIT_MEMLOCK\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max); break;
caseRLIMIT_NOFILE:
printf("RLIMIT_NOFILE\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_NPROC:
printf("RLIMIT_NPROC\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_RSS:
printf("RLIMIT_RSS\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max); break;
caseRLIMIT_STACK:
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

printf("RLIMIT_STACK\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
caseRLIMIT_LOCKS:
printf("RLIMIT_LOCKS\t%ld\t\t%ld\n",rl.rlim_cur,rl.rlim_max);
break;
default:
break;
}
}

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg5.c
Govind@Govind-Latitude-E5450:~/0S$ g++ - pg5 pg5.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg5
Resource Name
Current Limit
Max Limit
RLIMIT_CPU
-1
-1
RLIMIT_FSIZE
-1
-1
RLIMIT_DATA
-1
-1
RLIMIT_STACK
8388608
-1
RLIMIT_RSS
-1
-1
RLIMIT_NPROC
31088
31088
RLIMIT_NOFILE
1024
1048576
RLIMIT_MEMLOCK
1027579904
RLIMIT_LOCKS
-1
-1
Error in getrlimit
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-6

OBJECTIVE – Write a program with an exit handler that outputs CPU usage.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/times.h>
#include <time.h>

staticvoidshowtimes(void){
time_t time1, time2;
time_t time_dif;
time1=time(NULL);//man time,
//time(NULL)returnscurrenttimeinseconds
printf("time1:%ld",time1);
sleep(5); // man sleeptime2
= time(NULL);
printf("\ntime2:%ld",time2);
time_dif=difftime(time2,time1);//man difftime
printf("\nTheprogramsleptfor:%ldseconds\n", time_dif);
}

intmain(void){
if(atexit(showtimes))//manatexit
{
fprintf(stderr,"Failedtoinstallshowtimesexithandler\n"); return
1;
}
/*restofmainprogramgoeshere*/
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg6.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg6 pg6.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg6
time1 1713985924
time2: 1713985929
The program slept for: 5 seconds
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-7
OBJECTIVE – Write a program that prints it’s & it’s parent’s process ID.
CODE–

#include<stdio.h>#in
clude<unistd.h> int
main (void)
{
printf("Iamprocess%ld\n",(long)getpid());//mangetpid
printf("Myparentis%ld\n",(long)getppid());
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg7.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg7 pg7.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg7
I am process 7316
My parent is 4753
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-8

OBJECTIVE – Write a program that prints out various user & group ID’s.

CODE–

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

intmain(void){
//mangetuid,mangetgid
printf("My real user ID is %5ld\n", (long)getuid());
printf("My effective user ID is %5ld\n", (long)geteuid());
printf("My real group ID is %5ld\n", (long)getgid());
printf("MyeffectivegroupIDis%5ld\n",(long)getegid());
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg8.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg8 pg8.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg8
My real user ID is
1000
My effective user ID is 1000
My real group ID is 1000
My effective group ID is 1000
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-9

OBJECTIVE – Write a program which uses fork to create a child process& then parent & child print their
respective process ID’s
CODE–

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>

int main(void) {
pid_t childpid;
childpid =
fork();if(childpid=
=-1){
perror("Failedtofork");
return 1;
}
if(childpid ==0) {
/*childcode */
printf("Iamchild%ld\n",(long)getpid());
}else{
/*parentcode*/
printf("Iamparent%ld\n",(long)getpid());
}
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg9.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg9 pg9.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg9
I am parent 7647
I am child 7648
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-10
OBJECTIVE – Write a program that creates a chain of n processes, where n is a command line argument.
CODE–
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>

intmain(intargc,char*argv[]){
pid_t childpid = 0;
inti,n;

if(argc !=2){
fprintf(stderr,"Usage:%sprocesses\n",argv[0]);
return 1;
}
n= atoi(argv[1]);

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


if((childpid=fork())==-1){
perror("Failed to fork");
return 1;
}elseif (childpid==0) {
/*Childprocess*/
break;
}
}

fprintf(stderr,"i:%dprocessID:%ldparentID:%ldchildID:%ld\n", i,
(long)getpid(), (long)getppid(), (long)childpid);

return 0;
}

OUTPUT :-

Govind@Govind-Latitude-E5450:~/0S$ gedit pg10.c


Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg10 pg10.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg10 5
1:1 process ID:7711 parent ID:7710 child ID:0 1:2
process ID:7712 parent ID: 7710 child ID:0 1:5
process ID:7710 parent ID: 4753 child ID:7714 1:3
process ID:7713 parent ID:7710 child ID:0 1:4
process ID:7714 parent ID: 7710 child ID:0
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-11

OBJECTIVE – Write a program that creates a fan of n processes where n is passed as a command line
argument.
CODE–

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

intmain(intargc,char*argv[]){
pid_t childpid = 0;
inti,n;

if(argc !=2){
fprintf(stderr,"Usage:%sprocesses\n",argv[0]);
return 1;
}

n = atoi(argv[1]);

for(i=1;i<n;i++){
if((childpid=fork())<=0)
break;
}

fprintf(stderr,"i:%dprocessID:%ldparentID:%ldchildID:%ld\n", i,
(long)getpid(), (long)getppid(), (long)childpid);

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg11.c
Govind@Govind-Latitude-E5450:~/05$ g++ -o pg11 pg11.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg11 5
1:1 process ID:7788 parent ID: 7787 child ID:0 1:2
process ID:7789 parent ID: 7787 child ID:0 1:5
process ID:7787 parent ID: 4753 child ID:7791 1:3
process ID:7790 parent ID: 7787 child ID:0 1:4
process ID:7791 parent ID: 7787 child ID:0
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-12

OBJECTIVE – Write a program to show that same opened file can be shared by both parent and child
processes

CODE–

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/wait.h>

intmain(){
FILE *fp;
int fd;
char ch;

fp=fopen("test","w"); if
(fp == NULL) {
perror("Erroropeningfile");
exit(1);
}

fprintf(fp,"%s\n","ThislineiswrittenbyPARENTPROCESS"); fflush(NULL);
fd=fork();

if (fd < 0) {
perror("ForkError");
exit(1);
}

if(fd==0){
fprintf(fp,"%s","ThislineiswrittenbyCHILDPROCESS\n");
fclose(fp);
fp=fopen("test","r"); if
(fp == NULL) {
perror("Erroropeningfile");
exit(1);
}
while((ch=getc(fp))!=EOF)
printf("%c", ch);
}

if(fd>0){
//man2 wait
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

if(fd!=wait(NULL)){
perror("Parentfailedtowaitduetosignalorerror");
return 1;
}
}

fclose(fp);
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg12.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg12 pg12.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg12
This line is written by PARENT PROCESS
This line is written by CHILD PROCESS
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-13
OBJECTIVE–Write a program that creates a child process to run ls –l

CODE–
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/wait.h>

int main(void) {
pid_t childpid;
childpid=fork();

if (childpid == -1) {
perror("Failedtofork");
return 1;
}

if(childpid ==0) {
/*childcode*/
execl("/bin/ls","ls","-l",NULL);//man2exec
perror("Child failed to exec ls");
return 1;
}

if(childpid!=wait(NULL)){
/*parentcode*/
perror("Parentfailedtowaitduetosignalorerror"); return
1;
}

return 0;
}

OUTPUT :-

Govind@Govind-Latitude-E5450:~/0S$ gedit pg13.c


Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg13 pg13.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg13
total 272
-rwxrwxr-x 1
Govind Govind 16352 Apr 25 00:17 pg1 -rwxrwxr-x 1
Govind Govind 16216 Apr 25 00:49 pg10 -rw-rw-r-- 1
Govind Govind 653 Apr 25 00:49 pg10.c -rwxrwxr-x 1
Govind Govind 16176 Apr 25 00:50 pg11 -rw-rw-r-- 1
Govind Govind 512 Apr 25 00:50 pg11.c -rwxrwxr-x 1
Govind Govind 16384 Apr 25 00:51 pg12 -rw-rw-r-- 1
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-14

OBJECTIVE–Write a program to create a zombie child and find its status using
system (ps) command.

CODE–

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include<signal.h>

intmain(){
int fd;
if((fd=fork())< 0){
printf("Errorincreatingchild\n"); exit(1);
}
if (fd == 0) {
kill(getpid(),SIGKILL);
}else{
sleep(2);
}
system("ps-f");
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg14.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg14 pg14.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg14
PPID C STIME TTY 4735 Apr24 pts/0 47530 00:55 pts/0 80010 00:55 pts/0 8001 00:55 pts/0 8003 0 00:55
pts/0
UID
PID
Govind
4753
Govind
8001
Govind
8002
Govind
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-15

OBJECTIVE–Write a program to copy a file.

CODE–

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

#defineBLKSIZE 1024
#defineREAD_FLAGS O_RDONLY
#defineWRITE_FLAGS(O_WRONLY|O_CREAT|O_EXCL)
#define WRITE_PERMS (S_IRUSR | S_IWUSR)

intcopyfile(intfromfd,inttofd){
char buf[BLKSIZE];
intbytesread;
intbyteswritten=0;
int totalbytes = 0;

for(;;){
while(((bytesread=read(fromfd,buf,BLKSIZE))== -1)&&(errno==EINTR)); /*Handle interruption by
signal */
if(bytesread<=0)/*Realerrororend-of-fileonfromfd*/
break;

char*bp= buf;
while(bytesread>0){
while(((byteswritten=write(tofd,bp,bytesread))== -1)&&(errno==EINTR));/*Handle interruption
by signal */
if(byteswritten<0)/*Realerrorontofd*/
break;
totalbytes+=byteswritten;
bytesread -= byteswritten;
bp += byteswritten;
}
if(byteswritten==-1)
break;/*Realerrorontofd*/
}

returntotalbytes;
}
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

intmain(intargc,char*argv[]){ int
bytes;
intfromfd,tofd;

if(argc !=3){
fprintf(stderr,"Usage:%sfrom_fileto_file\n",argv[0]);
return 1;
}

if((fromfd=open(argv[1],READ_FLAGS))==-1){
perror("Failed to open input file");
return 1;
}

if((tofd=open(argv[2],WRITE_FLAGS,WRITE_PERMS))== -1){
perror("Failed to create output file");
close(fromfd);
return 1;
}

bytes=copyfile(fromfd, tofd);
printf("%dbytescopiedfrom%sto%s\n",bytes,argv[1], argv[2]);

close(fromfd);
close(tofd);

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg15.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg15 pg15.c
Govind@Govind-Latitude-E5450:~/0S$ cat > file1
this is file1
^C
Govind@Govind-Latitude-E5450:~/0S$ ./pg15 file1 file2 14 bytes copied from file1 to file2
Govind@Govind-Latitude-E5450:~/0S$ cat file2
this is file1
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-16

OBJECTIVE – Write a program for which output is automatically directed to a named file rather than on
to the console
CODE–

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

intmain(){
int fd;
if((fd=open("program16",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))<0){ perror("Error in
opening file");
exit(1);
}
close(1);
dup(fd);
printf("GovindLohar\n");
close(fd);
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg16.c
Govind@Govind-Latitude-E5450:~/05$ g++ -o pg16 pg16.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg16
Govind@Govind-Latitude-E5450:~/0S$ sudo cat program16
Govind
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-17

OBJECTIVE – Write a program that redirects standards output to the file my.file (or Write a program that
do the following operation cat XYZ > myfile). Using dup2 rather than dup.
CODE–

#include <fcntl.h>
#include <stdio.h>
#include<sys/stat.h>
#include <unistd.h>

#define CREATE_FLAGS (O_WRONLY | O_CREAT | O_APPEND)


#defineCREATE_MODE(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

intmain(void){
int fd;
fd=open("program17.txt",CREATE_FLAGS,CREATE_MODE); if
(fd == -1) {
perror("Failedtoopenprogram17.txt");
return 1;
}
if(dup2(fd,STDOUT_FILENO)==-1){
perror("Failedtoredirectstandardoutput");
return 1;
}
if(close(fd)== -1) {
perror("Failedtoclosethefile");
return 1;
}
printf("OK\n");//Outputwillberedirectedtoprogram17.txt
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg17.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg17 pg17.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg17
Govind@Govind-Latitude-E5450:~/0S$ cat program17.txt
OK
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-18

OBJECTIVE – Write a program to create an empty directory using system calls

CODE–

#include <stdio.h>
#include <stdlib.h>
#include<sys/stat.h>

intmain(intargc,char*argv[]){ if
(argc != 2) {
printf("Usage:%sdirectory\n",argv[0]);
exit(1);
}

if (mkdir(argv[1], 0744) != 0) {
perror("ErrorinMakingDirectory");
exit(1);
}

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg18.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg18 pg18.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg18 dir1
Govind@Govind-Latitude-E5450:~/0S$ sudo ls -ld dir1
[sudo] password for Govind:
drwxr--r-- 2 Govind Govind 4096 Apr 25 14:33 dir1
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-19
OBJECTIVE – Write a program to remove a directory using system call
CODE–

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

intmain(intargc,char*argv[]){ if
(argc != 2) {
fprintf(stderr,"Usage:%s<file>\n",argv[0]);
fprintf(stderr, "Too few arguments\n");
exit(1);
}

if (remove(argv[1]) != 0) {
perror("ErrorinRemovingFile");
exit(1);
}

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ ls -d */
dir1/
Govind@Govind-Latitude-E5450:~/0S$ gedit pg19.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg19 pg19.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg19 dir1
Govind@Govind-Latitude-E5450:~/0S$ sudo ls -ld dir1
ls: cannot access 'dir1': No such file or directory
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-20
OBJECTIVE – Write a program to output current working directory.
CODE–

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

#ifndef PATH_MAX
#definePATH_MAX255
#endif

intmain(void){
charmycwd[PATH_MAX];

if (getcwd(mycwd, PATH_MAX) == NULL) {


perror("Failedtogetcurrentworkingdirectory");
return 1;
}

printf("Currentworkingdirectory:%s\n",mycwd);

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg20.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg20 pg20.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg20
Current working directory: /home/Govind/os
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-21
OBJECTIVE – Write a program to list files in a directory.
CODE–
#include<dirent.h>
#include <errno.h>
#include <stdio.h>

intmain(intargc,char*argv[]){
struct dirent *direntp;
DIR*dirp;

if(argc !=2){
fprintf(stderr,"Usage:%sdirectory_name\n",argv[0]);
return 1;
}

if((dirp=opendir(argv[1]))==NULL){
perror("Failed to open directory");
return 1;
}

while((direntp=readdir(dirp))!=NULL){ printf("%s\n",
direntp->d_name);
}

if (closedir(dirp) == -1) {
perror("Failedtoclosedirectory");
return 1;
}

return 0;
}

OUTPUT :-

Govind@Govind-Latitude-E5450:~/0S$ gedit pg21.c


Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg21 pg21.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg21 dir1
file2 file1
file3
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-22
OBJECTIVE – Write a program that returns true if a given file is a directory & false otherwise.

CODE–

#include <stdio.h>
#include <time.h>
#include<sys/stat.h>

intmain(intargc,char*argv[]){ if
(argc != 2) {
fprintf(stderr,"Usage:%s<file_or_directory>\n",argv[0]); return
1;
}

structstat statbuf;
if(stat(argv[1],&statbuf)==-1){
perror("Failedtogetstatusoffile/directory"); return
1;
}

if (S_ISDIR(statbuf.st_mode)) {
printf("%s:isadirectory\n",argv[1]);
}else{
printf("%s:isafile\n",argv[1]);
}

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg22.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg22 pg22.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg22 dir1
dir1: is a directory
Govind@Govind-Latitude-E5450:~/0S$ ./pg22 file1
file1: is a file
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-23
OBJECTIVE – Write a program that can display the type of a given file like regular, directory etc.
CODE–
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>

intmain(intargc,char*argv[]){
struct stat statbuff;
intcheck;

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

check=stat(argv[1],&statbuff); if
(check != 0) {
perror("Erroringettingfilestatus");
exit(1);
}

if(S_ISREG(statbuff.st_mode)){
printf("Regular File\n");
}elseif(S_ISDIR(statbuff.st_mode)){
printf("Directory\n");
}elseif(S_ISCHR(statbuff.st_mode)){
printf("Character Device\n");
}else{
printf("OtherFile\n");
}

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg23.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg23 pg23.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg23 dir1
Directory
Govind@Govind-Latitude-E5450:~/0S$ ./pg23 file1
Regular File
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-24
OBJECTIVE – Write a program to display the permission of a given file
CODE–

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

intmain(intargc,char*argv[]){
struct stat statbuff;
intcheck;

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

check=stat(argv[1],&statbuff); if
(check != 0) {
perror("Erroringettingfilestatus");
exit(1);
}

//Checkpermissionsforowner
if((statbuff.st_mode&S_IRUSR)==S_IRUSR)
printf("Owner has Read Permission\n");
if((statbuff.st_mode&S_IWUSR)==S_IWUSR)
printf("Owner has Write Permission\n");
if((statbuff.st_mode&S_IXUSR)==S_IXUSR)
printf("OwnerhasExecutePermission\n");

//Checkpermissionsforgroup
if((statbuff.st_mode&S_IRGRP)==S_IRGRP)
printf("Group has Read Permission\n");
if((statbuff.st_mode&S_IWGRP)==S_IWGRP)
printf("Group has Write Permission\n");
if((statbuff.st_mode&S_IXGRP)==S_IXGRP)
printf("Group has Execute Permission\n");

//Checkpermissionsforothers
if((statbuff.st_mode&S_IROTH)==S_IROTH)
printf("Others has Read Permission\n");
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

if((statbuff.st_mode&S_IWOTH)==S_IWOTH)
printf("Others has Write Permission\n");
if((statbuff.st_mode&S_IXOTH)==S_IXOTH)
printf("Others has Execute Permission\n");

return 0;
}

OUTPUT:-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg24.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg24 pg24.c
Govind@Govind-Latitude-E5450:~/os$ ./pg24 file1
Owner has Read Permission
Owner has Write Permission
Group has Read Permission
Group has Write Permission
Others has Read Permission
Govind@Govind-Latitude-E5450:~/0S$ ./pg24 dir1
Owner has Read Permission
Owner has Write Permission
Owner has Execute Permission
Group has Read Permission
Group has Write Permission
Group has Execute Permission
Others has Read Permission
Others has Execute Permission
Govind@Govind-Latitude-E5450:~/0S$ ./pg24 file2
Owner has Read Permission
Owner has Write Permission
Owner has Execute Permission
Group has Execute Permission
Others has Execute Permission
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-25

OBJECTIVE – Write a program to execute the equivalent of ls –l | sort –n +4.


CODE–

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

int main(void) {
pid_tchildpid;
int fd[2];

if(pipe(fd)==-1){
perror("Failedtosetuppipeline");
return 1;
}

if((childpid=fork())==-1){
perror("Failed to fork");
return 1;
}

if(childpid==0){//Childprocess(ls)
if(dup2(fd[1],STDOUT_FILENO)==-1){
perror("Failedtoredirectstdoutofls"); return
1;
}

if (close(fd[0]) == -1 || close(fd[1]) == -1) {


perror("Failedtocloseextrapipedescriptorsonls");
return 1;
}

execl("/bin/ls","ls","-l",NULL);
perror("Failed to exec ls");
return 1;
}else{//Parent process (sort)
if(dup2(fd[0],STDIN_FILENO)== -1){
perror("Failedtoredirectstdinofsort");
return 1;
}

if(close(fd[0])== -1||close(fd[1])==-1) {
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

perror("Failedtocloseextrapipefiledescriptorsonsort"); return
1;
}

execl("/bin/sort","sort","-n","+4",NULL);
perror("Failed to exec sort");
return 1;
}
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg25.c
Govind@Govind-Latitude-E5450:~/0S$ g++ -o pg25 pg25.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg25
total 532
Govind Govind -rw- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rwx--x--x 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1 '
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-rw-r-- 1
Govind Govind -rw-r--r-- 1
3 Apr 25 14:31 program17.txt 13 Apr 25 01:03 program16 14 Apr 25 00:57 file1 14 Apr 25 00:57 file2 18 Apr 25
00:19 pg2input.txt 77 Apr 25 00:52 test 177 Apr 25 00:43 pg7.c 323 Apr 25 14:33 pg18.c 329 Apr 25 00:55 pg14.c
346 Apr 25 00:45 pg8.c 361 Apr 25 14:39 pg20.c 381 Apr 25 01:00 pg16.c 403 Apr 25 14:35 pg19.c 411 Apr 25 00:22
pg3.c 424 Apr 25 00:47 pg9.c 433 Apr 25 00:10 pg1.c 512 Apr 25 00:50 pg11.c 525 Apr 25 14:44 pg22.c 560 Apr 25
00:29 pg4.c 577 Apr 25 14:40 pg21.c
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-26
OBJECTIVE – Write a program to handle SIGUSR1 and SIGUSR2 signal..
CODE–

#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
#include<unistd.h>

voidfun(int);

int main() {
if(signal(SIGUSR1,fun)==SIG_ERR){
printf("HandlerforSIGUSR1notregistered\n");
exit(1);
}

if (signal(SIGUSR2, fun) == SIG_ERR) {


printf("HandlerforSIGUSR2notregistered\n");
exit(1);
}

while (1)
pause();//include<unistd.h>

return 0;
}

voidfun(inti){
if(i==SIGUSR1){
printf("ReceivedSIGUSR1interrupt\n");
}elseif(i==SIGUSR2){
printf("ReceivedSIGUSR2interrupt\n");
}
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg26.c
Govind@Govind-Latitude-E5450:~/05$ ps -aux | grep pg26
Govind@Govind-Latitude-E5450:~/os$ gcc -o pg26 pg26.c
Govind 4139 0.0 0.0 2640 988 pts/0 S+
Govind@Govind-Latitude-E5450:~/os$ ./pg26
Received SIGUSR1 interrupt
Received SIGUSR2 interrupt
./pg26 Govind
4141 0.0 0.0 17732 2432 pts/1 S+ grep -color=auto pg26
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-27
OBJECTIVE – Write a program which suspends itself till it receives a SIGALARM signal.
CODE–

#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
#include<unistd.h>

void sig_alrm(int);

intmain(intargc,char*argv[]){
if(signal(SIGALRM,sig_alrm)==SIG_ERR){
printf("FailedtoregistersignalhandlerforSIGALRM\n"); exit(1);
}

alarm(5);
pause();

return 0;
}

voidsig_alrm(intsig){
if(sig==SIGALRM){
printf("WakeUp\n");
}
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg27.c
Govind@Govind-Latitude-E5450:~/0S$ gcc -o pg27 pg27.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg27
Wake Up
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-28
OBJECTIVE – Write a program which prints the second’s part of current time whenever the SIGALRM
signal is received by the program.
CODE–
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

voidsig_hand(int);

int main() {
int i = 1;
pid_tpid;

if(signal(SIGALRM,sig_hand)==SIG_ERR){
printf("FailedtoregistersignalhandlerforSIGALRM\n"); exit(1);
}

while(i<=5){ i++;
pid=getpid();
sleep(2);
kill(pid,SIGALRM);
}
return0;
}

voidsig_hand(intsig){
struct tm *t;
time_ttt;

if(sig==SIGALRM){
tt= time(NULL);
t = localtime(&tt);
printf("%d\n",t->tm_sec);
}
}
OUTPUT :-

Govind@Govind-Latitude-E5450:~/05$ gedit pg28.c


Govind@Govind-Latitude-E5450:~/05$ gcc -o pg28 pg28.c
Govind@Govind-Latitude-E5450:~/05$ ./pg28
43
45
47
49
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-29

OBJECTIVE – Write a Program to print the entries of passwd file for a given user name or user ID.
CODE–

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>

intmain(){
charu_name[10];
char ch;
uid_t u_id;
structpasswd*p;

printf("EnterYourChoice\n");
printf("Whetheryouwanttoenter UNAMEorUID?(NorI):");
scanf("%c",&ch);//Addedspacebefore%ctoignoreleadingwhitespace

if (ch == 'N' || ch == 'n') {


printf("EnterUNAME:");
scanf("%s", u_name);
p=getpwnam(u_name); if
(p != NULL) {
printf("\n%s\n%s\n%d\n%d\n%s\n%s\n%s\n",p->pw_name,p->pw_passwd, p-
>pw_uid,
p->pw_gid,p->pw_gecos,p->pw_dir,p->pw_shell);
}else{
printf("Usernotfound.\n");
}
}elseif(ch=='I'||ch=='i'){
printf("Enter UID: ");
scanf("%d", &u_id);
p=getpwuid(u_id); if
(p != NULL) {
printf("\n%s\n%s\n%d\n%d\n%s\n%s\n%s\n",p->pw_name,p->pw_passwd, p-
>pw_uid,
p->pw_gid,p->pw_gecos,p->pw_dir,p->pw_shell);
}else{
printf("Usernotfound.\n");
}
}else{
printf("WrongChoice\n");
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg29.c
Govind@Govind-Latitude-E5450:~/0S$ gcc -o pg29 pg29.c
Govind@Govind-Latitude-E5450:~/05$ ./pg29
Enter Your Choice
Whether you want to enter UNAME or UID? (N or I): N Enter UNAME: Govind
Govind
X
1000
1000
Govind,,,
/home/Govind /bin/bash
Govind@Govind-Latitude-E5450:~/0S$ ./pg29
Enter Your Choice
Whether you want to enter UNAME or UID? (N or I): I Enter UID: 1001
Govind Sharma
X
1001
1001
Govind Sharma,,, /home/GovindSharma /bin/bash
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM-30
OBJECTIVE – Write a program to print the details of the system

CODE–

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

intmain() {
charg_name[10];
gid_t gid;
charch;
structgroup*g;

printf("EnterYourChoice:\nEnterGroupName(N)\nEnterGroupID(I)\n");
printf("Enter Choice: ");
scanf("%c",&ch);//Addedaspacebefore%ctoskipwhitespacecharacters

switch(ch){
case 'N':
case'n':
printf("EnterGNAME:");
scanf("%s", g_name);
g=getgrnam(g_name); if
(g == NULL) {
perror("Errorgettinggroupbyname");
exit(1);
}
printf("\n%s%s%d\n",g->gr_name,g->gr_passwd,g->gr_gid); break;
case'I':
case'i':
printf("EnterGID:");
scanf("%d", &gid);
g=getgrgid(gid); if
(g == NULL) {
perror("ErrorgettinggroupbyID");
exit(1);
}
printf("\n%s%s%d\n",g->gr_name,g->gr_passwd,g->gr_gid); break;
default:
printf("WrongChoice\n");
}
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ id
uid=1000 (Govind) gid=1000 (Govind) groups=1000 (Govind), 4(adm), 24(cdrom), 27(sudo), 30(dip),
46(plugdev), 122(lpadmin), 134(lxd), 135(sambashare)
Govind@Govind-Latitude-E5450:~/0S$ gedit pg30.c
Govind@Govind-Latitude-E5450:~/0S$ gcc -o pg30 pg30.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg30
Enter Your Choice:
Enter Group Name (N)
Enter Group ID (I) Enter Choice: N
Enter GNAME: Govind
Govind x 1000
Govind@Govind-Latitude-E5450:~/0S$ ./pg30
Enter Your Choice:
Enter Group Name (N)
Enter Group ID (I)
Enter Choice: I
Enter GID: 1001
GovindSharma x 1001
Govind@Govind-Latitude-E5450:~/0S$
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

PROGRAM–31,32

OBJECTIVE – Write Two Programs:


31.Write a program which Reads what is written to a named pipe & writes it to standard output.
32.Write an informative message to a named pipe.

CODE1–

//Server
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include<sys/types.h>
#define BLKSIZE 1024

intcopyfile(intfromfd,inttofd){
char buf[BLKSIZE];
ssize_tbytesread,byteswritten;
ssize_t totalbytes = 0;

while((bytesread=read(fromfd,buf,BLKSIZE))>0){
char *bp = buf;
while((byteswritten=write(tofd,bp,bytesread))>0){
totalbytes += byteswritten;
bytesread-=byteswritten; bp
+= byteswritten;
}
if(byteswritten==-1){
perror("write");
return -1;
}
}

if(bytesread==-1){
perror("read");
return -1;
}

returntotalbytes;
}
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

intmain(intargc,char*argv[]){ int
requestfd;

if(argc !=2){
fprintf(stderr,"Usage:%sfifoname>logfile\n",argv[0]);
return 1;
}

//Createanamedpipetohandleincomingrequests
if(mkfifo(argv[1],0666)==-1&&errno!=EEXIST){
perror("mkfifo");
return 1;
}

//Openaread/writecommunicationendpointtothepipe
if((requestfd=open(argv[1],O_RDWR))==-1){
perror("open");
return 1;
}

if(copyfile(requestfd,STDOUT_FILENO)==-1){
perror("copyfile");
return 1;
}

close(requestfd);
return 0;
}

CODE2–

//Client
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include<unistd.h>
#include<sys/stat.h>#defi
ne FIFOARG 1
#definePIPE_BUF1024 //DefinePIPE_BUFifnot defined

intmain(intargc,char*argv[]){
time_t curtime;
int len;
charrequestbuf[PIPE_BUF];
COLLEGEOFTECHNOLOGYANDENGINEERING,MPUAT,UDAIPUR(RAJ.)
Name- Govind Sharma Class -B.Tech, IIIyr, CSE Sem-VI Batch- A

Subject–OperatingSystems(CS-361)

intrequestfd;
if(argc!=2){/*nameofserverfifoispassedonthecommandline */
fprintf(stderr,"Usage:%sfifoname\n",argv[0]);
return 1;
}
if((requestfd=open(argv[FIFOARG],O_WRONLY))==-1){
perror("Client failed to open log fifo for writing");
return 1;
}
curtime=time(NULL);
snprintf(requestbuf,PIPE_BUF,"%d:%s",(int)getpid(),ctime(&curtime));
len = strlen(requestbuf);
if(write(requestfd,requestbuf,len)!=len){
perror("Client failed to write");
return1;
}
close(requestfd);
return 0;
}

OUTPUT :-
Govind@Govind-Latitude-E5450:~/0S$ gedit pg31.c
Govind@Govind-Latitude-E5450:~/0S$ gcc -o pg31 pg31.c
Govind@Govind-Latitude-E5450:~/0S$ ./pg31 fifoname> logfile
^C
Govind@Govind-Latitude-E5450:~/0S$
cat logfile
5311: Thu Apr 25 16:39:43 2024
Govind@Govind-Latitude-E5450:~/0S$

pg32
Govind@Govind-Latitude-E5450:~/0S$ gedit pg32.c
Govind@Govind-Latitude-E5450:~/0S$ gcc -o pg32 pg32.c
Govind@Govind-Latitude-E5450:~/05$ ./pg32 fifoname
Govind@Govind-Latitude-E5450:~/0S$

You might also like