0% found this document useful (0 votes)
258 views3 pages

Extend Above Experiment To Explore The Following System Calls: Open, Read, Write, Close, Getpid, Setpid, Getuid, Getgid, Getegid, Geteuid. Code

This document explores system calls related to file I/O and process IDs in Linux. It demonstrates opening, writing, reading, and closing files using open(), write(), read(), and close(). It shows getting and setting process IDs using getpid(), setuid(), getuid(), getgid(), getegid(), and geteuid(). The code samples create a file, write "Hello World" to it, read it back, and modify the effective user ID using setuid().

Uploaded by

atharva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
258 views3 pages

Extend Above Experiment To Explore The Following System Calls: Open, Read, Write, Close, Getpid, Setpid, Getuid, Getgid, Getegid, Geteuid. Code

This document explores system calls related to file I/O and process IDs in Linux. It demonstrates opening, writing, reading, and closing files using open(), write(), read(), and close(). It shows getting and setting process IDs using getpid(), setuid(), getuid(), getgid(), getegid(), and geteuid(). The code samples create a file, write "Hello World" to it, read it back, and modify the effective user ID using setuid().

Uploaded by

atharva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

6.

Extend above experiment to explore the following system calls: open,read, write,
close, getpid, setpid, getuid, getgid, getegid, geteuid.

CODE :-
#include<stdio.h>
#include<sys/stat.h>//system status file
#include <fcntl.h> //file control
int main()
{
//creating file
// dat file is a generic data file that stores information specific to the application it refers to.
// S_IREAD|S_IWRITE: System information bits for read and write on file
int fd = creat("datafile.dat",S_IREAD|S_IWRITE);
if(fd==-1)
printf("Error in opening file\n");
else
{
printf("File opened for read/write\n");
printf("File currently empty\n");
}
close(fd);
int sz;

//writing file
// O_RDONLY:Open for reading only.
//O_WRONLY:Open for writing only.
//O_RDWR:Open for reading and writing
// O_CREAT:If the file exists, this flag has no effect except as noted under O_EXCL //below.
Otherwise, the file is created;
//O_TRUNC:If the file exists and is a regular file, and the file is successfully opened //O_RDWR
or O_WRONLY, its length is truncated to 0
//0644:drw-r--r--

fd = open("datafile.dat", O_WRONLY | O_CREAT | O_TRUNC, 0644);


if (fd < 0)
{
perror("r1");//prints the system generated error r1:-------
exit(1);
}
//size of “hello world’’
sz = write(fd, "Hello World\n", strlen("Hello World\n"));

printf("called write(% d, \"Hello World\\n\", %d)."


" It returned %d\n", fd, strlen("Hello World\n"), sz);
close(fd);

//allocating buffer
char *c = (char *) calloc(100, sizeof(char));

//opening file in readonly mode


fd = open("datafile.dat", O_RDONLY);

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

/* Read() tells the operating system to read "size" bytes from the file opened in file descriptor
"fd", and to put those bytes into the location pointed to by "buf". It returns how many bytes were
actually read. */

sz = read(fd, c, 11);
printf("called read(% d, c, 10). returned that %d bytes were read.\n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c);
exit(0);
}

OUTPUT :-

lab308-23@sumatishazain:~$ ./exe
File opened for read/write
File currently empty
called write( 3, "Hello World\n", 12). It returned 12
called read( 3, c, 10). returned that 11 bytes were read.
Those bytes are as follows: Hello World

CODE :-
#include <stdio.h>
#include <sys/types.h>// defines the data types
#include <unistd.h>
/* unistd.h is the C/C++ header file that is your code's entry point to various constant, type and
function declarations that comprise the POSIX operating system API. */
int main(void)
{
printf("The process id: %d\n",getpid());
printf("User id: %d,Effective User id: %d\n",getuid(),geteuid());
printf("Group id: %d,Effective Group id: %d\n",getgid(),getegid());
setuid(6000);
printf("After setuid = 6000\n");
printf("User id: %d,Effective User id: %d\n",getuid(),geteuid());
return 0;
}

OUTPUT :-
lab308-23@sumatishazain:~$ gcc -o exe2 exp62.c
lab308-23@sumatishazain:~$ ./exe2
The process id: 4543
User id: 1000,Effective User id: 1000
Group id: 1000,Effective Group id: 1000
After setuid = 6000
User id: 6000,Effective User id: 6000

You might also like