Sunteți pe pagina 1din 3

CS 2203: Operating Systems Spring 2013

Center for Advnace Studies in Engineering (CASE) Computer Science Program


Lab 04 Instructions prepared by:Shehreyar Rashid, Waheed Iqbal
Instructors:

Lab 5
4: File Permissions and Named Pipes

Objectives
• Learn about file and directories permissions in Linux.
• Learn to use pipe operator (|) in Linux.

• Implement Named Pipe for inter process communication.

File Permissions
In operating system, files and directories have specific permissions e.g., read, write, or execute. In this
section, we explore file and directory permissions in Linux operating system. File and directory permissions
are divided into three main entities, owner, group, and other users of the system. Lets identify the file
and folder permissions:
1. Launch a terminal and execute ls -l. The output would be similar to as follows:
-rwxrwxr-x 1 shehreyar shehreyar 7161 2013-01-21 13:13 hello.c.
Figure 1 explains a sample access permission for each user, group, and other users of the system. The
first character in access permission for each file or folder indicates whether the file is a directory (d) or
not (-). The next nine slots are divided into three sets, each of length 3. The first set of three indicates
the owner access, the next set of three indicates the group access, and the last set of three indicates
the access for everybody else (others). The maximum access is represented by rwx, indicating read,
writes, and execute. Whenever a dash (-) appears, an access permission has not been given.

Figure 1: Folder permission to the owner, group, and other users access specifications.

2. Make sure to understand the file and directory permission concept in the Linux.
3. The access permissions can be changed using chmod command. Try to read the manual for chmod
command. Lets practice to change file permissions:
• Create a new file named “test.txt”. Now identify the default permission to the file.
• chmod o+w test.txt (give write permission to others).
• chmod o-w test.txt (remove write permission from others).
• chmod a+w test.txt (give write permission to all).

1
• chmod a-w test.txt (remove write permission to others).
• chmod u+x test.txt (give execute permission to owner).
• chmod go-x test.txt (remove execute permission from group and others).
4. Make sure to practice and get comfortable with changing file permissions.

Pipe Operator (|) in Linux


The pipe operator (|) use to send standard output of one Linux command to another Linux command as a
standard input. Lets practice pipe operator in Linux:
• ls | wc (output of ls reads as input by wc command).
• ls | sort (output of ls reads as input by sort command).
• ls -l | gedit (output of ls loads by gedit into a new document).

Named Pipes
In the labs, we have discussed ordinary pipes which is used to communicate between parent and child
processes. In this lab, we implement Named Pipe to communicate between two different processes.
1. Create a new file named fifo server.c and type in the following code:

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

int main()
{

int server_to_client;
char *myfifo = "/tmp/fifo";
char buf[100];
mkfifo("/tmp/fifo", 0666);
server_to_client = open(myfifo, O_RDONLY);

printf("Server is ready!\n");
read(server_to_client, buf, 100);
printf("Received string: %s", buf);

close(server_to_client);
unlink(myfifo);
return 0;
}

2. Create a another file named fifo client.c and type in the following code:

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

2
int main()
{
int client_to_server;
char *myfifo = "/tmp/fifo";
char str[100] = "Test Message From Client\n";

client_to_server = open(myfifo, O_WRONLY);


write(client_to_server, str, sizeof(str));

close(client_to_server);
return 0;
}

3. Compile the server and client programs. You need to open two terminals to run server and client
programs simultaneously.

4. The lab instructor will help you to understand the program.

In-lab Exercises
1. Update the Named Pipe program to send multiple messages to the server.

2. Update the Named Pipe program to make it bidirectional. Server must send an acknowledgment
message to the client. Hint! You may need to create two FIFO queues.

Submission Guidelines
Once you are done with In-lab Exercises, make sure to show them to the lab instructors. You also need
to submit a report through email to [shehreyar.rashid237@gmail.com
course canvas account ] in a format available at the course
home page.

S-ar putea să vă placă și