0% found this document useful (0 votes)
16 views

CS2

networking programming lecture at bits

Uploaded by

jarison_mander
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CS2

networking programming lecture at bits

Uploaded by

jarison_mander
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

POSIX Feature Test Macros

• _POSIX_JOB_CONTROL
• _POSIX_SAVED_IDS
• _POSIX_CHOWN_RESTRICTED
• _POSIX_NO_TRUNC

1
FIPS- Federal Information
Processing Standards
• Published by NIST
• NIST is department within US department of Commerce
• FIPS served as standards for the US federal agencies acquiring
computer systems.
• Latest FIPS : FIPS-151-1 based on POSIX.1 standards.
• FIPS is more stricter than FIPS.
Reason: Features optional to POSIX are Mandatory in FIPS
1. Must support BSD Style JOB Control.
2. Must allow changes to UID and GID via seteuid() and setegid().
3. Only super user must be allowed to change the ownership
information.
4. There should not be silent truncation of long pathnames.
5. NGROUP_MAX must be defined to atleast 8.
6. Read and Write APIs should return number of bytes read/written.
7. Newly created process must inherit the GID and UID from its parent.
2
X/Open Standards
• X/Open : is a consortium formed by a group of European
Companies.
• Objective: To develop the common operating system interface for
their system.
• X/Open published two guidelines
1. XPG3 in 1994
2. XPG4 in 1995
• These Guidelines were based on the POSIX.1 and ANSI C
standards.
• X/Open added additional features to their guidelines.
• Few computer vendors like HP, Novel, IBM, OSF initiated a project
called as COSE- Common Open Software Environment.
3
Kernel
• The core operating system
Two Meanings:
• Central software managing a computer’s
hardware resources and standard software
tools, such as command-line interpreters,
graphical user interfaces, file editors.
• More narrowly, central software that manages
and allocates computer resources (i.e., the
CPU, RAM, and devices).
4
Kernel Tasks
• Process scheduling:
Unix uses the preemptive scheduling.

• Memory management:
Employs the virtual memory management.
Advantages:
1. Process are isolated from one another and from kernel.
2. Entire process need not be present in the memory for execution. Only a part of the
processes can be loaded and executed.

• File System:
The kernel provides a file system on disk, allowing files to be created, retrieved, updated,
deleted.

• Process Management:
Kernel creates a new process, allocates resource for its execution, when process terminates
it frees the resources.
5
Kernel Tasks Continued…
• Access to Devices:
Provides access to the physical devices like mouse, keyboard,
disk, printers etc., through the device drivers.

• Networking:
Kernel transfer and receives the network message (packets) on
behalf of the user process (i,e., it does the routing of the packet to the
target system)

• Provision for System Call:


Kernel provides system call also called as kernel entry points.
User process can access the kernel resources through the system calls
6
User and Kernel Mode
• User Mode: CPU has access only to the process related data. (Process
address space).

• Kernel Mode/Supervisory Mode/Protected Mode: CPU has access to the


process related and also the kernel resources.

• Note:
1. CPU is switched to the kernel mode when process invokes the execution
of the API.
2. CPU can perform few operations only in the kernel mode (Examples :-
executing the halt instruction to stop the system, accessing the memory-
management hardware, and initiating device I/O operations.)

7
Process versus kernel views of the system
• Process-oriented:
Running system has numerous processes.
Process does not know
-- when it will be scheduled for execution.
-- where it is located in the memory.
-- when it receives the signal.
-- how to communicate with the other process.
-- cannot create itself another child process.
-- cannot terminate itself.
-- cannot communicate directly with the input and output
devices.
8
Process versus kernel views of the system
continued…
• Kernel Orientation:
Running system has one kernel that manages all the processes.
Kernel facilitates the following.
-- decides when to schedule the process for execution and how long.
-- decides where to locate the process in the memory.
-- decides when to generate and send the signals to the running
process.
-- provides a mechanism for the processes to communicate.
-- provides a mechanism for the process to create a child process
upon the request made.
-- decides when to forcibly terminate the running process and
terminate on the request made by the process.

9
Shell
• Special program which read the user commands and
executes appropriate programs.
• Such programs are called as Command Interpreter.
• Different Shells
1) Bourne Shell (sh):
- Oldest Shell, Written by Steve Bourne
- Standard shell for 7th edition UNIX.
- Familiar Features included I/O redirection, pipes,
variables, manipulation of environment variables,
background command execution.

10
Shell Continued…
2) C Shell (csh):
- Written by Bill Joy at university of California at
Berkeley
- Name derived from C programming language.
- Has many interactive features which were
unavailable in Bourne Shell like Command History, Job
Control.
- C Shell is not backward compatible with Bourne
Shell.

11
Shell Continued…
3) Korn shell (ksh):
- Written by David Korn at AT&T Bell
Laboratories
- This is successor to Bourne shell
- Backward compatible with the Bourne
shell is maintained.
- Incorporates the same interactive
features as the C shell.
12
Shell Continued…
4) Bourne again shell (bash):
- Written by Brian Fox and Chet Ramey
- Re-implementation of the Bourne shell
- Preserved interactive features similar to
those available in the C and Korn shells.
- Bash is the most widely used shell
nowadays.

13
Users and Groups
• Every user on the system is uniquely identified and
users belong to groups.
• User Identification:
– Login name (username)
– User ID (Numeric value)
– Group ID (Numeric value)
– Home Directory
– Login shell
Note: 1. These information is defined by a line in the password
file (/etc/passwd).
2. For security reasons the passwords are encrypted
and stored in the separate file called as shadow password file
14
Users and Groups Conti…
• Group Identification:
- Required for administrative purpose.
- In early UNIX user could be a member of
only one group.
- Latest UNIX with POSIX standards allows
a user to belong to more than one group
simultaneously.

15
File Types
• Regular Files/Ordinary Files/Data Files
• Directory Files
• Pipe Files/FIFO Files
• Character Device Files
• Block Device Files
• Symbolic Link Files
• Socket Files

16
Regular Files/Ordinary Files/Data Files

• These are plain text file or the binary file.


• File is a storage for the collection of related
information.
• Both text file and binary files can be executed by
providing appropriate execute permission.
• Operations on regular files:
- can be created (ed, vi, cat, gedit etc)
- can be browsed/viewed (ed, vi, cat, gedit etc)
- can be modified (vi, cat, gedit)
- can be removed (rm)
17
Directory Files

• Directory is a collection of related files.


• Created using mkdir
• Removed using rmdir.
• Also created using mknod on few early versions of unix OS.
• Directory is like table like structure.

• Default file . and ..


• Directory can contain other directories.
18
Pipe Files/FIFO Files

• Special files used for IPC within the system.


• Created using mkfifo/mknod
• Operates in FIFO order.
• Removed using rm.
• Pipe size is PIPE_BUF
• Opened only through the read and write APIs

19
Character Device Files

• Device files which transfers the data byte by


byte (i,e character a time) between process
and the physical device.
• Created using mknod
mknod <char_dev_file> c <major No.> <Minor No.>
• Examples: line printers, modems

20
Block Device Files

• Device files which transfers the data block by


block (i,e sequence of characters a time)
between process and the physical device.
• Created using mknod
mknod <char_dev_file> b <major No.> <Minor No.>
• Examples: hard disks, floppy drives

21
Symbolic Link Files

• Special file which contains the reference to


the original file/another file.
• Created using ln –s command.
• Gets a different inode number
• Can link file across the file system
• Can link even the directory files.
usage:
ln –s <original file> <new file>
22

Socket Files
Special files used for IPC within and across the systems.
Example:
• #include <fcntl.h>
• #include <sys/un.h>
• #include <sys/socket.h>
• #include <sys/stat.h>
• #include <sys/types.h>
• #include <unistd.h>

• int main(int argc, char *argv[])


• {
• // The following line expects the socket path to be first argument
• char * mysocketpath = argv[1];
• // Alternatively, you could comment that and set it statically:
• //char * mysocketpath = "/tmp/mysock";
• struct sockaddr_un namesock;
• int fd;
• namesock.sun_family = AF_UNIX;
• strncpy(namesock.sun_path, (char *)mysocketpath, sizeof(namesock.sun_path));
• fd = socket(AF_UNIX, SOCK_DGRAM, 0);
• bind(fd, (struct sockaddr *) &namesock, sizeof(struct sockaddr_un));
• close(fd);
• return 0;
• }

23
Others
• File Names
• Path Names
• Current Working Directory
• File Ownership and Permissions

24
File Attributes
struct stat
{
dev_t st_dev; /* file system ID */
ino_t st_ino; /* file inode number */
mode_t st_ mode; /* contains file type and permission */
nlink_t st_nlink; /* hard link count */
uid_t st_uid; /* file user ID */
gid_t st_gid; /* file group ID */
dev_t st_rdev; /*contains major and minor device#*/
off_t st_size; /* file size in bytes */
time_t st_atime; /* last access time */
time_t st_mtime; /* last modification time */
time_t st_ctime; /* last status change time */
};
25
Kernel Support for files

26
File I/O Model
• Universality of I/O.
• System calls.
– Open()
– Read()
– Write()
– Close()
– lseek()

27
Open() system call
• This is used to establish a connection between a process and a file i.e. it is used to open an existing file for data
transfer function or else it may be also be used to create a new file.
• The returned value of the open system call is the file descriptor (row number of the file table), which contains
the inode information.
• Prototype:

#include<unistd.h>
#include<sys/types.h>
#include<sys/fcntl.h>
int open(const char *pathname, int accessmode, mode_t permission);

Generally the access modes are specified in . Various access modes are:
O_RDONLY - open for reading file only
O_WRONLY - open for writing file only
O_RDWR - opens for reading and writing file.
Access Mode can be Appended with:
O_APPEND - Append data to the end of file.
O_CREAT - Create the file if it doesn’t exist
O_EXCL - Generate an error if O_CREAT is also specified and the file already exists.
O_TRUNC - If file exists discard the file content and set the file size to zero bytes.
O_NONBLOCK - Specify subsequent read or write on the file should be non-blocking.

28
Open () conti…
• Third argument is used when creating file
using open () to specify the access permission.

29
Open () Conti..
• Example:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int fd,fd1,fd2;
fd=open("/home/CSE/CS054/a1.txt",O_RDONLY);
fd1=open("/home/CSE/CS054/a2.txt",O_RDONLY);
if(fd==-1)
perror("Open failed");
else
printf("File descriptor for a1.txt=%d\n",fd);
if(fd1==-1)
perror("Open failed");
else
printf("File descriptor for a2.txt=%d\n",fd1);
close(fd);
fd2=open("/home/CSE/CS054/tsl.c",O_RDONLY);
if(fd2==-1)
perror("Open failed");
else
printf("File descriptor for tsl.c=%d\n",fd2);
return 0;
30
}
Open() conti…
• Example 2:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd;
fd=open("/home/CSE/CS054/a1.txt",O_WRONLY|O_CREAT|O_EXCL,0741);
if(fd==-1)
perror("Open failed");
else
printf("File descriptor=%d\n",fd);
return 0;
}
31
Open () conti…
• Example 3:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>

int main()
{
int fd;
fd=open("/home/CSE/CS054/a1.txt",O_WRONLY|O_CREAT|O_EXCL,S_IRWXU|S_IRGRP|S_IXOTH);
if(fd==-1)
perror("Open failed");
else
printf("File descriptor=%d\n",fd);
return 0;
}

32
Read () system call
• The read function fetches a fixed size of block
of data from a file referenced by a given file
descriptor.
The prototype of read function is:
#include <unistd.h>
#include<sys/types.h>
size_t read(int fdesc, void *buf, size_t nbyte);

33
Read () Conti…
Example 1:

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
int fd,rdc;
char buff[11];
fd=open("/home/CSE/CS054/a1.txt",O_RDONLY);
if(fd==-1)
{
perror("Open failed");
exit(0);
}
else
{
rdc=read(fd,buff,10);
if(rdc==-1)
perror("read failed");
buff[10]='\0';
printf("Data read=%s",buff);
}
return 0;
34
}
Read () Conti…
Example 2: Reading standard input
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int rdc;
char buff[11];
rdc=read(0,&buff,10);
if(rdc==-1)
perror("read failed");
buff[10]='\0';
printf("Data read=%s",buff);
return 0;
}

35
Write () System call
• The write system call is used to write data into a
file.
• The write function puts data to a file in the form of
fixed block size referred by a given file descriptor.
• The prototype of write is
#include<unistd.h>
#include<sys/types.h>
ssize_t write(int fdesc, const void *buf, size_t size);

36
Write () conti…
• Example 1:

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int fd,wdc;
char buff[]="Hello CISCO,how are you?";
fd=open("/home/CSE/CS054/a3.txt",O_WRONLY|O_CREAT,0777);
if(fd==-1)
{
perror("Open failed");
exit(0);
}
wdc=write(fd,buff,sizeof(buff));
if(wdc==-1)
{
perror("Write failed");
exit(0);
}
printf("%d bytes of data is successfully written into file a3.txt\n",wdc);
return 0;
} 37
Write() conti….
• Example 2:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
int wdc;
char buff[]="Hello BIT,how are you?";
wdc=write(1,buff,sizeof(buff));
if(wdc==-1)
{
perror("Write failed");
exit(0);
}
return 0;
}

38
Close () System Call
• The close system call is used to terminate the
connection to a file from a process.
• The prototype of the close is
#include<unistd.h>
int close(int fdesc);

39
Close () Example:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int fd,fd1,fd2;
fd=open("/home/CSE/CS054/a1.txt",O_RDONLY);
fd1=open("/home/CSE/CS054/a2.txt",O_RDONLY);
if(fd==-1)
perror("Open failed");
else
printf("File descriptor for a1.txt=%d\n",fd);
if(fd1==-1)
perror("Open failed");
else
printf("File descriptor for a2.txt=%d\n",fd1);
close(fd);
fd2=open("/home/CSE/CS054/tsl.c",O_RDONLY);
if(fd2==-1)
perror("Open failed");
else
printf("File descriptor for tsl.c=%d\n",fd2);
return 0;
}

40
Command Line Arguments

int main(int argc, char *argv[])

• argc  command line argument count


including file name
• argv  stores the arguments itself

41
Directory Specific APIs
• Opendir()
• Readdir()
• Rewinddir()
• For BSD UNIX
#include <sys/dir.h>
Struct direct
{
Char d_name[];
Long d_ino;
}
• For POSIX
#include<dirent.h>
Struct Dirent
{
Char d_name[];
Long d_ino;
}

42
Example on Directory Specific API
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#if defined (BSD) && (!_POSIX_SOURCE)
#include <sys/dir.h>
typedef struct direct Dirent;
#else
#include<dirent.h>
typedef struct dirent Dirent;
#endif
43
int main(int argc,char*argv[])
{
DIR *fdesc;
Dirent *dp;
int i,cnt;
while(--argc>0)
{
fdesc=opendir(*++argv);
if(fdesc==0)
{
if(mkdir(*argv,S_IRWXU|S_IRGRP|S_IROTH)==-1)
perror("mkdir error");
else
printf("%s created\n",*argv);
continue;
}
for(i=0;i<2;i++)
{
if(i)
printf("Files under directory %s\n",*argv);
for(cnt=0;dp=readdir(fdesc);)
{
if(i)
printf("%s\n",dp->d_name);
if(strcmp(dp->d_name,".") && strcmp(dp->d_name,".."))
cnt++;
}
if(!cnt)
{
rmdir(*argv);
break;
}
rewinddir(fdesc);
}
closedir(fdesc);
}
}

44

You might also like