Unix System Overview part_2 Flashcards

1
Q

describe utime function

A

the access time and the modifiaction time of a file can be changed with the utime function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
#include  
int utime(const char *pathname, const struct utimbuf *times);
what can this function return?
A

Returns 0 if ok.

and -1 on error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the structure used by the utime function?

A
struct utimbuf {      
time_t actime;    
/* access time */       
time_t modtime;  
/* modification time */   
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The operation of utime function, and the privileges required to execute it, depend on whether the times argument is NULL.
if it is not null then?

A

If times is a non-null pointer, the access time and the modification time are set to the values in the structure pointed to by times. For this case, the effective user ID of the process must equal the owner ID of the file, or the process must be a superuser process. Merely having write permission for the file is not adequate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what field is automatically updated when the utime function is called.

A

we are unable to specify a value for the changed-status time, st_ctime—the time the i-node was last changed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
what does this return?
#include  
int mkdir(const char *pathname, mode_t mode);
A

return 0 if OK

and return -1 on error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what function creates directories?

A

mkdir.
This function creates a new, empty directory. The entries for dot and dot-dot are automatically created. The specified file access permissions, mode, are modified by the file mode creation mask of the process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what functions delete directories?

A

rmdir
An empty directory is deleted with the rmdir function. Recall that an empty directory is one that contains entries only for dot and dot-dot.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
#include 
int rmdir(const char *pathname);
what does this return?
A

Return 0 if OK

and return -1 on error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a common mistake that occurs sometimes from using mkdir?

A

A common mistake is to specify the same mode as for a file: read and write permissions only. But for a directory, we normally want at least one of the execute bits enabled, to allow access to filenames within the directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

explain how rmdir works?

A

If the link count of the directory becomes 0 with this call, and if no other process has the directory open, then the space occupied by the directory is freed. If one or more processes have the directory open when the link count reaches 0, the last link is removed and the dot and dot-dot entries are removed before this function returns. Additionally, no new files can be created in the directory. The directory is not freed, however, until the last process closes it. (Even though some other process has the directory open, it can’t be doing much in the directory, as the directory had to be empty for the rmdir function to succeed.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
what does this return?
#include  
int chdir(const char *pathname);
A

returns 0 if OK

returns -1 on Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
what does this return?
#include 
int fchdir(int filedes)
A

returns 0 if OK

returns -1 on Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what functions can change the current working directory of the calling process?

A

chdir or fchdir fucntions.

we can specify the new current working directory either as a pathname or through an open file descriptor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

the fchdir function provides us with an easy way to accomplish a task?

A

The fchdir function provides us with an easy way to accomplish this task. Instead of calling getcwd, we can open the current directory and save the file descriptor before we change to a different location in the file system. When we want to return to where we started, we can simply pass the file descriptor to fchdir.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
what does this return?
#include 
DIR *opendir(const char *pathname);
A

return pointer if OK

return NULL if error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what does this return?

struct dirent *readdir(DIR *dp);

A

returns pointer if OK

returns Null at end of directory or error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
what does this return?
void rewinddir(DIR *dp);
A

returns 0 if OK

returns -1 of error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
what does this return?
int closedir(DIR *dp);
A

returns 0 if OK

returns -1 on error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
what does this return?
long telldir(DIR *dp);
A

returns current location in directory associated with dp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
what does this return?
void seekdir(DIR *dp, long loc);
A

nothing its void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what are the 3 streams that are predefined and automatically available to a process? and what header are they defined in?

A
standard input
standard output
standard error.
defined in 
note: there streams refer to the same files as the file descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is the goal of buffering provided by the standard I/O library?

A

is to use the minimum number of read and write calls. (Recall Figure 3.5, where we showed the amount of CPU time required to perform I/O using various buffer sizes.) Also, it tries to do its buffering automatically for each I/O stream, obviating the need for the application to worry about it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

describe fully buffered

A

Fully buffered. In this case, actual I/O takes place when the standard I/O buffer is filled. Files residing on disk are normally fully buffered by the standard I/O library. The buffer used is usually obtained by one of the standard I/O functions calling malloc (Section 7.8) the first time I/O is performed on a stream.
The term flush describes the writing of a standard I/O buffer. A buffer can be flushed automatically by the standard I/O routines, such as when a buffer fills, or we can call the function fflush to flush a stream. Unfortunately, in the UNIX environment, flush means two different things. In terms of the standard I/O library, it means writing out the contents of a buffer, which may be partially filled. In terms of the terminal driver, such as the tcflush function in Chapter 18, it means to discard the data that’s already stored in a buffer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

describe line buffered

A

Line buffered. In this case, the standard I/O library performs I/O when a newline character is encountered on input or output. This allows us to output a single character at a time (with the standard I/O fputc function), knowing that actual I/O will take place only when we finish writing each line. Line buffering is typically used on a stream when it refers to a terminal: standard input and standard output, for example.
Line buffering comes with two caveats. First, the size of the buffer that the standard I/O library is using to collect each line is fixed, so I/O might take place if we fill this buffer before writing a newline. Second, whenever input is requested through the standard I/O library from either (a) an unbuffered stream or (b) a line-buffered stream (that requires data to be requested from the kernel), all line-buffered output streams are flushed. The reason for the qualifier on (b) is that the requested data may already be in the buffer, which doesn’t require data to be read from the kernel. Obviously, any input from an unbuffered stream, item (a), requires data to be obtained from the kernel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

what requirements do buffering characteristics do ISO C require?

A

Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device. Standard error is never fully buffered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

what defult buffering types for the standard I/Os

A

standard error is always unbuffered.
all other streams are line buffered if they refer to a terminal device.
otherwise they are fully buffered

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what happens if we specify an unbuffered stream?

A

the fuf and size arguments are ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

what happens if we specify fully buffered or line buffered?

A

buf and size can be optionally specify a buffer and its size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
what does this do and return?
#include 
int fflush(FILE *fp);
A

returns 0 if OK.
returns EOF on error.
this function allows us at any time, we can force a stream to be flushed. this function causes any unwritten data for the stream to be passed to the kernel.
As a special case, if fp is null this function causes all output streams to be flushed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
what does this do and return?
#include  
void setbuf(FILE *restrict fp, char *restrict buf);
A

return 0 if OK
return nonzero on error
used to change the buffering. This function must be called after the stream has been opened. we can turn buffering on or off.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
what does this do and return?
#include 
int setvbuf(FILE *restrict fp, char *restrict buf,  int mode,             size_t size);
A

return 0 if OK
return nonzero on error
used to change the buffering. This function must be called after the stream has been opened. we specify exactly which type of buffering we want. done by these mode arguments:
_IOFBF fully buffered _IOLBF line buffered _IONBF unbuffered

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
what does this do and return?
#include  
FILE *fopen(const char *restrict pathname, const char *restrict type);
A

return file pointer if OK
return NULL on error.
opens a specified file.
part of ISO C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
what does this do and return?
#include
FILE *freopen(const char *restrict pathname, const char *restrict type,
FILE *restrict fp);
A

return file pointer if OK
return NULL on error.
opens a specified file on a specified stream, closing the stream first if its already open. If the stream previously had an orientation, freopen clears it. This function is typically used to open a specified file as one of the predefined streams: standard input, standard output, or standard error.
part of ISO C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q
what does this do and return?
#include 
FILE *fdopen(int filedes, const char *type);
A

The fdopen function takes an existing file descriptor, which we could obtain from the open, dup, dup2,
fcntl, pipe, socket, socketpair, or accept functions, and associates a standard I/O stream with the
descriptor. This function is often used with descriptors that are returned by the functions that create
pipes and network communication channels. Because these special types of files cannot be opened with
the standard I/O fopen function, we have to call the device-specific function to obtain a file descriptor,
and then associate this descriptor with a standard I/O stream using fdopen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q
#include 
int fclose(FILE *fp);
what does this do and return?
A

returns 0 if OK
returns EOF on error
Any buffered output data is flushed before the file is closed. Any input data that may be buffered is discarded. If the standard I/O library had automatically allocated a buffer for the stream, that buffer is released.
When a process terminates normally, either by calling the exit function directly or by returning from the main
function, all standard I/O streams with unwritten buffered data are flushed, and all open standard I/O streams are closed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

describe Character-at-a-time I/O

A

type of unformatted I/O.
We can read or write one character at a time, with the standard I/O functions handling all the buffering, if the stream is buffered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

describe Line-at-a-time I/O

A

type of unformatted I/O
If we want to read or write a line at a time, we use fgets and fputs. Each line is terminated with a newline character, and we have to specify the maximum line length that we can handle
when we call fgets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

describe Direct I/O

A

type of unformatted I/O
This type of I/O is supported by the fread and fwrite functions. For each I/O operation, we
read or write some number of objects, where each object is of a specified size. These two functions are
often used for binary files where we read or write a structure with each operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q
what does this do and return?
#include 
int fgetc(FILE *fp);
A

cannot be implemented as as a macro.
Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of
fgetc as an argument to another function. Calls to fgetc probably take longer than calls to getc, as it usually takes more time to call a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q
what does this do and return?
#include 
int ferror(FILE *fp);
A
returns nonzero (true) if condition is true (when error occurs)
returns 0 (false) otherwise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q
what does this do and return?
#include 
int feof(FILE *fp);
A
returns nonzero (true) if condition is true (when end of file is reached)
returns 0 (false) otherwise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q
what does this do and return?
#include 
int ungetc(int c, FILE *fp);
A

after reading from a stream, we can push back characters by calling this function.
returns c if OK,
returns EOF on error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q
what does this do and return?
#include 
void clearerr(FILE *fp);
A

the error flag and end-of-file flag are cleared by calling this function.
does not return anything since void.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q
what does this do and return?
#include 
char *fgets(char *restrict buf, int n, FILE *restrict fp);
A

returns buf if OK.
returns null on end-of-file or error.
the line-at-a-time I/O provides the function.
specify the address of the buffer to read the line into. reads from the specified stream. with fgets we have to specify the size of the buffer n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q
what does this do and return?
#include
int fputs(const char *restrict str, FILE *restrict fp);
A

returns non-negative value if OK.
returns EOF on error.
The function fputs writes the null-terminated string to the specified stream. The null byte at the end is not
written. Note that this need not be line-at-a-time output, since the string need not contain a newline as the last
non-null character. Usually, this is the case—the last non-null character is a newline—but it’s not required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q
what does this do and return?
#include 
size_t fread(void *restrict ptr, size_t size, size_t nobj, FILE *restrict fp);
A

return number or objects read.
For the read case, this number can be less
than nobj if an error occurs or if the end of file is encountered.

48
Q
what does this do and return?
#include 
size_t fwrite(const void *restrict ptr, size_t size, size_t nobj, FILE *restrict fp);
A

return number or objects written.

For the write case, if the return value is less than the requested nobj, an error has occurred.

49
Q

what are the 2 parplems with binary I/O (fread and fwrite)

A
  1. The offset of a member within a structure can differ between compilers and systems, because of
    different alignment requirements. Indeed, some compilers have an option allowing structures to be
    packed tightly, to save space with a possible runtime performance penalty, or aligned accurately, to
    optimize runtime access of each member. This means that even on a single system, the binary layout of a
    structure can differ, depending on compiler options.
  2. The binary formats used to store multibyte integers and floating-point values differ among machine
    architectures.
50
Q
what does this do and return?
#include 
long ftell(FILE *fp);
A

returns current file position indicator if OK.

returns -1L on error.

51
Q
what does this do and return?
#include 
int fseek(FILE *fp, long offset, int whence);
A

returns 0 if OK.

returns nonzero on error.

52
Q
what does this do and return?
#include 
int fgetpos(FILE *restrict fp, fpos_t *restrict pos);
A
return 0 if OK.
return nonzero on error.
function stores the current value of the file's position indicator in the object pointed by pos.
53
Q
what does this do and return?
#include
int fsetpos(FILE *fp, const fpos_t *pos);
A

return 0 if OK.
return nonzero on error.
the value from fgetpos can be used in a later call in this function to reposition the stream to that location.

54
Q
what does this do and return?
#include 
int printf(const char *restrict format, ...);
A
returns number of characters output if OK.
returns negative value if output error.
function writes to the standard output.
55
Q
what does this do and return?
#include
int scanf(const char *restrict format, ...);
A

returns number of input items assigned.
returns EOF if input error or end-of-file before any conversion.
is used to parse and input string and convert character sequences into variables of specified types.

56
Q
what does this do and return?
#include 
int fileno(FILE *fp);
A

return the file descriptor associated with the stream.
Each standard I/O stream has an associated file descriptor and we can obtain the descriptor for a stream by calling this function..
We need funciton if we want to call the dup or fcntl functions.

57
Q

name 2 limitations pipes have

A
  1. Historically, they have been half duplex (i.e., data flows in only one direction). Some systems now
    provide full-duplex pipes, but for maximum portability, we should never assume that this is the case.
  2. Pipes can be used only between processes that have a common ancestor. Normally, a pipe is created by a
    process, that process calls fork, and the pipe is used between the parent and the child.
58
Q

name IPC that get around some of the limitations

A

We’ll see that FIFOs get around the second limitation, and that UNIX domain sockets and named STREAMS-based pipes get around both limitations.

59
Q

what is the most commonly used of IPC?

A

the half-duplex pipes.
note: Every time you type
a sequence of commands in a pipeline for the shell to execute, the shell creates a separate process for each
command and links the standard output of one to the standard input of the next using a pipe.

60
Q
what does this do and return?
#include 
int pipe(int filedes[2]);
A

return 0 if OK.
return -1 on error.
creates a pipe. NOTE: Two file descriptors are returned through the filedes argument: filedes[0] is open for reading, and filedes[1] is open for writing. The output of filedes[1] is the input for filedes[0].

61
Q

describe the half-duplex pipe process.

A

Two ways to picture a half-duplex pipe. one shows the two ends of the pipe connected in a single process. The other emphasizes that the data in the pipe flows
through the kernel.

62
Q

describe the half-duplex pipe call with the fork call as well

A

Normally, the process that calls pipe then calls fork, creating an
IPC channel from the parent to the child or vice versa.What happens after the fork depends on which direction of data flow we want. For a pipe from the parent to the child, the parent closes the read end of the pipe (fd[0]), and the child closes the write end (fd[1]).

63
Q

what 2 rules apply when one end of a pipe is closed

A
  1. If we read from a pipe whose write end has been closed, read returns 0 to indicate an end of file after
    all the data has been read. (Technically, we should say that this end of file is not generated until there are
    no more writers for the pipe. It’s possible to duplicate a pipe descriptor so that multiple processes have
    the pipe open for writing. Normally, however, there is a single reader and a single writer for a pipe.
    When we get to FIFOs in the next section, we’ll see that often there are multiple writers for a single
    FIFO.)
  2. If we write to a pipe whose read end has been closed, the signal SIGPIPE is generated. If we either
    ignore the signal or catch it and return from the signal handler, write returns –1 with errno set to EPIPE.
64
Q

give an example where the code creates a pipe between a parent and its child and to send data down the pipe.

A

we called read and write directly on the pipe descriptors. What is more interesting is
to duplicate the pipe descriptors onto standard input or standard output. Often, the child then runs some other
program, and that program can either read from its standard input (the pipe that we created) or write to its
standard output (the pipe).

65
Q
what does this do and return?
#include 
FILE *popen(const char *cmdstring, const char *type);
A

returns: file pointer if OK.
returns: Null on error.
this function does a fork and exec to execute the cmdstring, and returns a standard I/O file pointer. If
type is “r”, the file pointer is connected to the standard output of cmdstring. If type is “w”, the file pointer is connected to the standard input of cmdstring.

66
Q
what does this do and return?
#include 
int pclose(FILE *fp);
A

returns: termination status of cmdstring
returns: -1 on error.
this function loses the standard I/O stream, waits for the command to terminate, and returns the termination status of the shell. If the shell cannot be executed, the termination status returned by pclose is as if the shell had executed exit(127).

67
Q

What happens if the caller of pclose has established a signal handler for SIGCHLD?

A

The call to waitpid from pclose would return an error of EINTR. Since the caller is allowed to catch this signal
(or any other signal that might interrupt the call to waitpid), we simply call waitpid again if it is interrupted by a caught signal.

68
Q
what does this do and return?
#include 
pid_t getpid(void);
A

returns: process ID of calling process.

69
Q
what does this do and return?
#include 
pid_t getppid(void);
A

returns: parent process ID of calling process.

70
Q
what does this do and return?
#include  
uid_t getuid(void);
A

returns: real user ID of calling process.

71
Q
what does this do and return?
#include  
uid_t geteuid(void);
A

returns: effective user ID of calling process.

72
Q
what does this do and return?
#include 
pid_t fork(void);
A

returns: 0 in child, process ID of child in parent.
returns: -1 on error.
The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process ID of the new child. The reason the child’s process ID is returned to the parent is that a process can have more than one child, and there is no function that allows a process to obtain the process IDs of its children.

73
Q

what are two normal cases for handling the descriptors after a fork?

A
  1. The parent waits for the child to complete. In this case, the parent does not need to do anything with its
    descriptors. When the child terminates, any of the shared descriptors that the child read from or wrote to
    will have their file offsets updated accordingly.
  2. Both the parent and the child go their own ways. Here, after the fork, the parent closes the descriptors
    that it doesn’t need, and the child does the same thing. This way, neither interferes with the other’s open
    descriptors. This scenario is often the case with network servers.
74
Q

list the properties of the parent that are inherited by the child.

A
- Open files
• Real user ID, real group ID, effective user ID, effective group ID
• Supplementary group IDs
• Process group ID
• Session ID
• Controlling terminal
• The set-user-ID and set-group-ID flags
• Current working directory
• Root directory
• File mode creation mask
• Signal mask and dispositions
• The close-on-exec flag for any open file descriptors
• Environment
• Attached shared memory segments
• Memory mappings
• Resource limits
75
Q

what are the differences between the parent and child processes?

A

• The return value from fork
• The process IDs are different
• The two processes have different parent process IDs: the parent process ID of the child is the parent; the
parent process ID of the parent doesn’t change
• The child’s tms_utime, tms_stime, tms_cutime, and tms_cstime values are set to 0
• File locks set by the parent are not inherited by the child
• Pending alarms are cleared for the child
• The set of pending signals for the child is set to the empty set

76
Q

what are the two main reasons for a fork to fail?

A

(a) if too many processes are already in the system, which usually means that something else is wrong, or (b) if the total number of processes for this real user ID exceeds the system’s limit.

77
Q

what are two uses for fork?

A
  1. When a process wants to duplicate itself so that the parent and child can each execute different sections
    of code at the same time. This is common for network servers—the parent waits for a service request from a client. When the request arrives, the parent calls fork and lets the child handle the request. The parent goes back to waiting for the next service request to arrive.
  2. When a process wants to execute a different program. This is common for shells. In this case, the child does an exec right after it returns from the fork.
78
Q

what are the five ways a process can terminate?

A
  1. Executing a return from the main function. this is equivalent to calling exit.
  2. Calling the exit function. This function is defined by ISO C and includes the calling of all exit handlers
    that have been registered by calling atexit and closing all standard I/O streams.
  3. Calling the _exit or _Exit function. ISO C defines _Exit to provide a way for a process to terminate
    without running exit handlers or signal handlers.
    In most UNIX system implementations, exit(3) is a function in the standard C library, whereas
    _exit(2) is a system call.
  4. Executing a return from the start routine of the last thread in the process. The return value of the thread
    is not used as the return value of the process, however. When the last thread returns from its start routine,
    the process exits with a termination status of 0.
  5. Calling the pthread_exit function from the last thread in the process.
79
Q

describe WIFEXITED()

A

marcos to examine the termination status returned by wait and waitpid.
True if status was returned for a child that terminated normally. In this case, we can execute WEXITSTATUS (status) to fetch the low-order 8 bits of the argument that the child passed to exit, _exit,or _Exit.

80
Q

describe a zombie process.

A

A process that has terminated, but whose parent has not yet waited for it

81
Q

what can a process that calls wait or waitpid do?

A

Block, if all of its children are still running
• Return immediately with the termination status of a child, if a child has terminated and is waiting for its
termination status to be fetched
• Return immediately with an error, if it doesn’t have any child processes

82
Q

what is the difference between wait() and waitpid()

A

The wait function can block the caller until a child process terminates, whereas waitpid has an option that prevents it from blocking.
• The waitpid function doesn’t wait for the child that terminates first; it has a number of options that control which process it waits for.

83
Q

what are 3 features that arent provided by the waitpid() and not the wait()

A
  1. The waitpid function lets us wait for one particular process, whereas the wait function returns the status of any terminated child. We’ll return to this feature when we discuss the popen function.
  2. The waitpid function provides a nonblocking version of wait. There are times when we want to fetch a child’s status, but we don’t want to block.
  3. The waitpid function provides support for job control with the WUNTRACED and WCONTINUED options
84
Q
what does this function do and return?
#include 
#include 
#include 
#include 
pid_t wait3(int *statloc, int options, struct rusage *rusage);
A

returns process ID if OK.
returns 0.
returns -1 on error.

85
Q
what does this function do and return?
#include 
#include 
#include 
#include 
pid_t wait4(pid_t pid, int *statloc, int options,
 struct rusage *rusage);
A

returns process ID if OK.
returns 0.
returns -1 on error.

86
Q

describe race conditions

A

a race condition occurs when multiple processes are trying to do something with shared data
and the final outcome depends on the order in which the processes run.

87
Q

what does execve do?

A

allows us to pass a pointer to an array of pointers to the environment strings. allow us to pass a pointer to an array of pointers to the environment strings.

88
Q
what does this do and return?
#include
int setuid(uid_t uid);
A

return 0 if OK
return -1 on error.
set the real user ID and effective user ID

89
Q
what does this do and return?
#include
int setgid(gid_t gid);
A

return 0 if OK.
return -1 on error.
set the real group ID and effective group ID

90
Q

There are rules for who can change the IDs.

A
  1. If the process has superuser privileges, the setuid function sets the real user ID, effective user ID, and saved set-user-ID to uid.
  2. If the process does not have superuser privileges, but uid equals either the real user ID or the saved setuser-ID, setuid sets only the effective user ID to uid. The real user ID and the saved set-user-ID are not changed.
  3. If neither of these two conditions is true, errno is set to EPERM, and –1 is returned.
91
Q

Interpreter files are useful for the following reasons.

A
  • They hide that certain programs are scripts in some other language.
  • Interpreter scripts provide an efficiency gain.
  • Interpreter scripts let us write shell scripts using shells other than /bin/sh.
92
Q

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

A

starts c program execution.
argv is array of pointers to arguments.
argc is number of command-line arguments

93
Q

what are the 5 normal ways for a process to terminate?

A
  1. Return from main
  2. Calling exit
  3. Calling _exit or _Exit
  4. Return of the last thread from its start routine
  5. Calling pthread_exit from the last thread
94
Q

what are the 3 abnormal ways for a process to terminate

A
  1. Calling abort
  2. Receipt of a signal
  3. Response of the last thread to a cancellation request
95
Q

describe the 2 functions that terminate a program normally

A

_exit which return to the kernel immediately, and exit, which performs certain cleanup processing and then returns to the kernel.

96
Q
what does this do and return?
#include 
int atexit(void (*func)(void));
A

returns 0 if OK
returns nonzero on error
With ISO C, a process can register up to 32 functions that are automatically called by exit. These are called
exit handlers

97
Q

memory layout

A

stack where automatic variables (local) are stored.
heap where dynamic memory allocation usually takes place.
uninitialized data segment where data is initialized by the kernal to arithmetic 0

98
Q

malloc

A

malloc, which allocates a specified number of bytes of memory. The initial value of the memory is
indeterminate

99
Q

calloc

A

calloc, which allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits.

100
Q

realloc

A

realloc, which increases or decreases the size of a previously allocated area. When the size increases, it
may involve moving the previously allocated area somewhere else, to provide the additional room at the
end. Also, when the size increases, the initial value of the space between the old contents and the end of
the new area is indeterminate.

101
Q

free()

A

The function free causes the space pointed to by ptr to be deallocated.

102
Q

what Three rules govern the changing of the resource limits.

A
  1. A process can change its soft limit to a value less than or equal to its hard limit.
  2. A process can lower its hard limit to a value greater than or equal to its soft limit. This lowering of the
    hard limit is irreversible for normal users.
  3. Only a superuser process can raise a hard limit.
103
Q
#include 
struct passwd *getpwuid(uid_t uid);
A

return pointer if ok.
return null on error.
The getpwuid function is used by the ls(1) program to map the numerical user ID contained in an i-node into a
user’s login name.

104
Q
#include 
struct passwd *getpwnam(const char *name);
A

return pointer if ok.
return null on error.
The getpwnam function is used by the login(1) program when we enter our login name.

105
Q
#include 
struct passwd *getpwent(void);
A

return pointer if ok.
return null on error.
We call getpwent to return the next entry in the password file.

106
Q

void setpwent(void);

A

The function setpwent rewinds whatever files it uses

107
Q

void endpwent(void);

A

and endpwent closes these files

108
Q

void setgrent(void);

A

The setgrent function opens the group file, if it’s not already open, and rewinds it

109
Q

struct group *getgrent(void);

A

The getgrent function reads the next entry from the group file, opening the file first, if it’s not already open.

110
Q

void endgrent(void);

A

The endgrent function closes the group file.

111
Q
#include 
int uname(struct utsname *name);
A

returns non-negative value if ok.
returns -1 on error.
the uname function to return information on the current host and operating system.

112
Q
#include 
time_t time(time_t *calptr);
A

return value of time if ok.
return -1 on error
The time function returns the current time and date.

113
Q
#include 
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
A

returns 0 always

The gettimeofday function provides greater resolution (up to a microsecond) than the time function.

114
Q
#include  
char *ctime(const time_t *calptr);
A

returns pointer to null-terminated string.
produce the familiar 26-byte string that is similar to the default output of the date(1) command:
Tue Feb 10 18:27:38 2004\n\0

115
Q
#include 
size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);
A

Returns: number of characters stored in array if room,
0 otherwise
The final time function, strftime, is the most complicated. It is a printf-like function for time values