Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the program so it does the following: a. In the beginning, the program prints out the system call numbers for sys_open, sys_close, sys_write, and

Modify the program so it does the following:

  • a. In the beginning, the program prints out the system call numbers for sys_open, sys_close, sys_write, and sys_getpid system calls. This can be achieved by simply printing the value of the respective system call macro (e.g., SYS_open).

  • b. Invokes the sys_getpid() system call -- a system call used by a process to retrieve its own id (an integer) -- and then prints it. sys_getpid() takes no parameters and returns an integer representing an ID of the current process.

  • Opens the file titled myfile.txt, directly invokes the sys_write() system call to write a string "Hello OS and hello system calls!", and then closes the file

#include
#include
#include
#include /* For SYS_xxx definitions */
#include
#include
#include
#include
#include

int main()
{

/* Issue a system call asking the operating system to open file file.txt.
* The first parameter, 5, tells the OS to execute a routine for opening
* the file. The rest of the arguments are parameters to the OS routine:
* the name of the file and the mode in which the file should be ipened
* (i.e. for reading, writing, and the file should be created if it does
* not exist.
*
* If successful, the system call will return the file descriptor,
* which is like a reference to the file opened file. It can be
* passed as parameter to system calls for reading and writing files,
* in order to read/write data to/from files.
*/
int fileDescriptor = syscall(SYS_open, "file.txt", O_RDONLY);

/* The buffer to store the read data */
char buffer[100];

/* Error checks (syscall returns a -1 on faliure) */
if(fileDescriptor < 0)
{
perror("syscall");
exit(-1);
}

/* Issue a system call to read 12 bytes of data from the file */
/* The calling convention for sys_read is
* @param fileDescriptor - the file descriptor representing
* an open file that we would like to ask the OS to read
* @param buffer - the buffer where the read data should be stored
* @param 12 - represents the number of bytes to read from the file.
* in this example, we would like to read 12 bytes of data.
* @return - sys_read will return the number of bytes that have
* actually been successfully read, 0 if we are at the end of the file,
* or -1 on error.
*/
if(syscall(SYS_read, fileDescriptor, buffer, 12) < 0)
{
perror("syscall");
exit(-1);
}

/* Null terminate the string in the buffer */
buffer[12] = '0';

fprintf(stderr, "Read: %s", buffer);

/* Issue a system call to close the file */
/* The calling conventions of SYS_close() are:
* @param fileDescriptor - the file descriptor representing
* the file we would like to close
*/
if(syscall(SYS_close, fileDescriptor) < 0)
{
perror("syscall");
exit(-1);
}

return 0;
}


Step by Step Solution

3.56 Rating (149 Votes )

There are 3 Steps involved in it

Step: 1

include include include include For SYSxxx definiti... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial Accounting

Authors: Walter Jr. Harrison, Charles T. Horngren, C. William Thomas, Greg Berberich, Catherine Seguin

6th Canadian edition

134564146, 978-0134141091, 134141091, 978-0134564142

More Books

Students also viewed these Accounting questions

Question

4. Pupils often misbehave in order to make the teacher look bad.

Answered: 1 week ago

Question

5. How is the auditory cortex like the visual cortex?

Answered: 1 week ago