OS Week9 Lab and Skill
OS Week9 Lab and Skill
OS Week9 Lab and Skill
Pre-Lab Task:
1. Write the function prototypes for the following calls
1) opendir( ) :-
11) ftruncate( ):-
2) readdir( ):-
12) ioctl( ) :-
3) closedir( ):- 13) link( ):-
4) stat( ):- 14) mknod( ):-
5) lstat( ):-
15) sync( ):-
6) chown( ):-
16) truncate( ):-
7) chmod( ):-
17) mount( ):-
8) fchown( ):-
18) getcwd( ):-
9) fchmod( ):-
19) chdir( ):-
10) fcntl( ):-
2. Write a program to print list of files and folders in column format (dir.c) and demonstrate directory
navigation with chdir and getcwd ?
i) //Print list of files and folders ii) //Demonstrate directory navigation
#include<stdio.h> with chdir and getcwd
#include<unistd.h> #include <stdio.h>
int main() #include <dirent.h>
{ int main(void)
char s[100]; {
printf("%s\n", getcwd(s, 100)); struct dirent *de;
chdir(".."); DIR *dr = opendir(".");
printf("%s\n", getcwd(s, 100)); if (dr == NULL)
return 0; {
} printf("Could not open current directory" );
Output: return 0;
C:\Users\KLH\Documents }
C:\Users\KLH while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
closedir(dr);
return 0;
}
Output:
My Music
My Pictures
ReadThisFirst.pdf
s.c
sg.cpp
1
In Lab Task:
Steps of the Program:
• Open the Terminal.
• Then, you can see the logged in user with the $ symbol next to the username.
• $ means you are logged in as a regular user and the # means you are root user.
Problem Description:
1. Write a system program that demonstrates chmod( ), chown( ) System calls for changing file permissions.
i) //chmod()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
ii) //chown()
#define _POSIX_SOURCE
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#undef _POSIX_SOURCE
#include <stdio.h>
void main() {
char fn[]="./temp.file";
FILE *stream;
struct stat info;
if ((stream = fopen(fn, "w")) == NULL)
perror("fopen() error");
else {
fclose(stream);
stat(fn, &info);
printf("original owner was %d and group was %d\n", info.st_uid,
info.st_gid);
if (chown(fn, 25, 0) != 0)
perror("chown() error");
else {
stat(fn, &info);
printf("after chown(), owner is %d and group is %d\n",
info.st_uid, info.st_gid);
}
unlink(fn);
}
}
Output:
1. original owner was 1000 and group was 1000
after chown(), owner is 1025 and group is 1000
2. umask returns the previous value of the mask. Unlike the shell’s umask statement, however,the umask system
call can’t display the current value of the mask without changing it. To use a workaround, store the current
mask by changing it to some arbitrary value, and then display it before restoring it. Write a Program that
Changes umask twice and checks effect on permissions.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILENAME "umask-example.temp"
void printFilePerms(void)
{
FILE *poutput = popen("ls -l " FILENAME, "r");
char buff[512];
while(fgets(buff, sizeof(buff), poutput)!=NULL)
printf("%s", buff);
pclose(poutput);
}
int main(void)
{
printf("Decimal %d and octal %d\n\n", 777, 0777);
printf("Trying to create a file with rwxrwxrwx permissions with open()\n");
createFile(0777);
printFilePerms();
printf("\n");
umask(0321);
unlink(FILENAME);
return 0;
}
Output:
Decimal 777 and octal 511
Trying to create a file with rwxrwxrwx permissions with open()
-rwxr-xr-x 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0000
Trying to create a file with rwxrwxrwx permissions with open()
-rwxrwxrwx 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0000
Trying to create a file with rwx------ permissions with open()
-rwx------ 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0077
Trying to create a file with rwxrwxrwx permissions with open()
-rwx------ 1 cse cse 0 Oct 23 10:22 umask-example.temp
Setting umask to 0077
Trying to create a file with rw-rw-rw- permissions with open()
-rw------- 1 cse cse 0 Oct 23 10:22 umask-example.temp
SKILL EXERCISE 9:
1. Write a system program that lists all in only directories - Uses S_ISDIR
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
Output:
List of Directory
.
hi
jk
hii
hello
int main()
{
struct flock fl;
int fd;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fd = open("jkk.txt", O_RDWR | O_EXCL);
fcntl(fd, F_SETLKW, &fl);
sleep(5);
printf(" release lock \n");
fl.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &fl);
}
Output:
after 5 seconds
release lock