CS 3377 exam 2

studied byStudied by 96 people
5.0(1)
get a hint
hint

How do you create a (unnamed) pipe?

1 / 119

Tags and Description

120 Terms

1

How do you create a (unnamed) pipe?

pipe(int ), where int is an array of 2 ints to hold file descriptors that will be associated with the pipe.

New cards
2

Assume your pipe fds are in fd[2]. What is fd[0], fd[1] for? If no files were opened previously in this program what numbers are they likely to take?

-fd[0] is the read fd.

-fd[1] is the write fd

-3, 4. Chooses minimum available fd; recall 0-2 are taken by standard input, output, and error.

New cards
3

What happens to a pipe after a fork?

Both parent and child can access both ends. Remember file descriptors for parent and child lead to the same destinations; one for pipe read, another for pipe write.

New cards
4

What is the best practice for a pipes between parent and child?

One pipe from parent to child; another from child to parent. After forking, close the end which the parent/ child respectively won’t use.

New cards
5

You want to exec a child process to a program that only reads to fds 0-2. What does dup(fd) do?

What about dup2(fd, destination)?

What should you do with any remaining fds open and why?

Also what do these dup functions return?

-Dup(fd) takes the fd, and opens the lowest numbered available next fd to have the same destination as the given fd. (In this case, you want 0-2)

-dup2(fd, destination) takes the fd, and replaces the destination fd to point to point to the same location as the fd you specified.

-In the case any remaining fds are pipes, you would cause a hangup of the parent. (For ex, a perpetually open pipe write end). Thus, you should closed unused fds.

*Note: both dup/dup2 return the newly created/ replaced fd. [-1 for error]

New cards
6

If the write end of a pipe is open, a read ___ . If the write end is closes, the pipe ____ .

Will block until something gets read. If the write end is closed, whatever’s left in the pipe will be read, and when that’s exhausted, you hit eof and stop being able to read things in.

This is critical to get eof-dependent processes to terminate (instead of having them get blocked at a read and run forever). Thus, why you should close unused pipe ends, so when the active write and is finished, you can close it and the process on the other end knows to stop.

New cards
7

What is the difference between execl and execvp?

What is the last entry of argv array (argv[argc])?

Execl - takes a pathname (can be relative, which is when you just type the filename), and most importantly, argv passed one by one as C-strings (with first being program name).

Execvp - uses PATH to find the file and assumes you pass a pointer to an argv C-string (pointer) array you already constructed.

-argv[argc] must be NULL. For execvp, consider how the exec function would know how man pointers in argv otherwise.

New cards
8

What do the ps -ef options do?

why can’t you use cut with space as delimiter?

What is the -p option?

The -u option?

-e lists all processes

-f gives pid, ppid, user, start time, and more details.

-ps output fields are formatted and aligned, not space delimited

-p specify a process to get information on

-u list’s a user’s processes.

New cards
9

What is the preferred way to create shell scripts (and why)?

What is the first line of any shell script?

touch scripFile, so then you can chmod ugo+x scriptFile to it. Execute is not by default on.

First line is: #! /bin/bash

Which tells bash to execute. (changing the second item will change what executers the script -- for instance, Python is an option).

New cards
10

How to create a C-struct, and refer to it for use later?

struct Name {

// entries

};

and for variables:

struct Name varl;

or list them after closing brace of struct.

New cards
11

Compare shell script vs traditional program?

A use of shell script?

-Programs use library functions

-Scripts use the shell and its commands

(convenient to give tedious command sequences to a computer to do); they’re not very powerful alone.

New cards
12

What is the purpose of the PATH environment variable?

Provides options for places to search for files. (You have to use ./script because your path is set to search in ~veerasam/bin if not found in your directory.)

New cards
13

How is a program vs scripts run, understood by the computer, size of executable?

-Both involve a call to executable name with (optional) arguments.

-scripts is interpreted, program is compiled.

-script is mostly just text stored in file, program binary is much larger.

New cards
14

A shell variable is assigned as?

Accessed as?

variable = [what you want to assign] to access, must reference as $variable

New cards
15

What does a ‘…’ expression assigned to a shell variable do?

Creates a separate process, executes the commands inside the backticks, and the results of those commands are assigned to the shell variables.

New cards
16

How does shell variable typing work?

You can assign characters, strings, or numbers. There is no explicitly enforced typing, although is the contents are wrong, tour scripts might run into errors. In a sense, it’s like the shell dumps the contents of the variable as a string wherever the variable is used and then interprets that expanded line and determines if it can execute or not.

New cards
17

How to access a command line argument in a shell script?

How about all of the aruments together, at once?

$1, $2, … for individual (technically ${1}, ${2}, … is more correct when arguments get into double digits), or $* for all.

New cards
18

What can you use bash -x for?

Printout every line of what the shell script executed, for debugging.

New cards
19

How to use the bash shell scripting if construct?

if [condition]

then

execute if true

else # optional

execute if false

if #marks end of if. MANDATORY

New cards
20

What are some bash shell script comparison operators?

How is a condition formed?

Between two values:

-ne not equal

-eq equal

-lt less than

-le less than or equal

-gt greater than

-ge greater than or equal

-z [String] tests if String is zero length.

condition sits inside [ condition ] - must have left/right spaces. For the between two values, left and right side must evaluate to a comparable value.

New cards
21

What do these mean: !!, $$, $LOGNAME

!! = last expression typed into command line replaces this

$$ = pid of calling process replaces it

$LOGNAME contains your login name

New cards
22

What are ways to evaluate arithmetic expressions in shell scripting?

‘ expr [expression]’, but since this is a command you must be careful to escape any special meaning symbols. You will also need to refer to variable contents as $var. ((expression)) requires assignment inside parentheses, but any group of letters here between operators is assumed to be taken as a variable. ($var is optional).

New cards
23

What is finger command, diff?

finger takes an argument of a username and prints out info about the user.

diff takes two files as arguments and finds the differences. To read diff’s output, < indicates unique to left file, > indicates unique to right file.

New cards
24

How to use the read utility in shell scripting?

read variableToReadInto

Note it reads lines at a time

New cards
25

How to use the while constructor in shell scripts?

while [ condition ]

do # to do if valid

done # MANDATORY

New cards
26

How to use the for each construct in shell scripts?

for { shell variable ] in $var do #to do for each token split by space

done # MANDATORY

New cards
27

How to use for construct in shell scripts?

for ((var=init; [condition];

[update var]))

do# to do on each iteration

done #MANDATORY

Syntax is very close to C

New cards
28

What is popen, pclose, and how to use?

popen allows you to specify a program to exec and what to do with a pipe program (i.e. five it input, take its output). it gives a FILE*.

pclose that FILE* to close the pipe

New cards
29

What can a named pope (FIFO) do that an unnamed pipe cannot?

-Exist independently of programs

-Don’t have to be passed on from a common ancestor. Just need to know name to use

New cards
30

In the filesystem, a FIFO is denoted with _ and has _ size?

p

0, it’s just a buffer

New cards
31

In a C program, you create and configure a FIFO with:

mkfifo(C-string name, octal permissions)

then chmod(C-string name, octal permissions)

-In the examples, the permissions in mkfifo didn’t work properly

New cards
32

How many writers and readers should a FIFO have?

1 reader, many writers. Too many readers is a bad idea as read side isn’t atomic but write side is.

New cards
33

What does getenv(“USER’) do?

Gets the USER environment variable (login name)

New cards
34

If a client becomes unresponsive, the server will block while trying to open the write end of the new client-server pope. How to make the server robust?

Make children that interact with clients, so they can get stuck.

New cards
35

S server’s child must exit after finishing. Why?

Otherwise they become a separate server. leading to multiple readers in a FIFO.

New cards
36

If you want a back and forth conversation between server and a client, but allow the server to also handle new clients simultaneously, you should:

For a child of the server, and create a new FIFO in the child to talk to that client. Like a bank: general queue for service (parent that assigns each client to a child), then particular employee to handle your case (child).

New cards
37

fflush and fprintf go well together with FIFOs because:

What’s written may get stuck in the buffer instead of written to the FIFO, fflush ensures that data is written. (For a deeper look, fprintf looks at the file type, and it unfortunately thinks FIFOs are a larger file so a large buffer is ideal.)

New cards
38

What are the results of these interactions:

  1. Server opens read end of FIFO but write end is still closed.

  2. Client opens write end of FIFO but read end is still closed.

  3. Client writes to a FIFO after read end has been closed.

  4. Server reads from FIFO after write end has been closed.

  5. Server reads from FIFO with open write end but no data.

  1. server blocks until the write end opened at least by one client

  2. Client blocks until the read end opened by the server

  3. Writing to a closed read-end FIFO will crash the program. (Same for regular pipes.)

  4. The remaining data in pipes is read out, then EOF is read.

  5. Read blocks until data is written

New cards
39

In process management, what do the following do?

  1. ctrl Z

  2. fg [job id] command

  3. bg [job id] command

  4. & symbol

  1. pauses foreground process and lets you interact with shell

  2. puts background job id into foreground

  3. Puts foreground job id into background

  4. starts a job in the background

New cards
40

What do threads share?

Not share?

-Share: global memory, file descriptors, code executable

-don’t share: registers/ stacks

New cards
41

How do threads and forks differ in terms of process ids?

-A thread is a part of a process, so shares the PID

-To give it an identification, threads get a lightweight process ID instead

-A fork creates a brand new process

New cards
42

the ps -fl option:

Show you LWP (lightweight process id) and NLWP (num of lightweight processes) aka thread pid and number of threads

New cards
43

Conceptually, how is execution of threads distributed across processor cores?

Compare that to a single-thread program

-Threads can be assigned to different cores

-A regular program is serialized and executes on one core

-Clearly, using all the cores in parallel is faster

New cards
44

When you start up a program, how many threads are there?

-1 thread. The program execution itself is a thread

New cards
45

What is lscpu?

List details about server CPUs

New cards
46

Describe a way to use threads to distribute a program’s workload?

Master thread assigns tasks to the other threads

New cards
47

When two threads are producing output, what can be said about the order of output if there is no syncronization?

Threads are given execution time by the system scheduler. Without synchronization, the order is unpredicable

New cards
48

What must a startup function for a thread look like? (Return type, parameters)?

void functionName (void args)

Must be void *

New cards
49

Thread ids are stored in the type:

pthread_t

New cards
50

What arguments does pthread_create take?

What changes after a call?

(pointer to a pthread_t, optional thread attributes [we use NULL], startup function, pointer to function arguments)

-Important note: the function arguments must be accessible from the one pointer, and can be NULL

Changes: Returns 0 on success, and the pthread_t that is pointed at is populated with thread info

New cards
51

What arguments does pthread_join take?

What changes after a call?

(pointer to pthread_t type, pointer to location to store return value)

-calling thread is blocked until specified thread returns

-location to store return value is populated with return value

New cards
52

Describe what went wrong in trouble2.c:

Asynchronous threads try to update a counter variable. Consider if thread A reads the counter, then thread B reads the counter before A increments it. Then, thread A updates the counter. However, since thread B has old data, when it increments what is read and updates the counter, it writes the same value thread A just wrote, with net effect wasting an increment. (Hence why the observed values could never exceed the expected count).

New cards
53

Describe how to create a mutex, and how to call pthread_mutex_lock and pthread_mutex_unlock ?

Can create a mutex statically with:

pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER;

Call the lock/unlock functions with a pointer to the mutex (struct).

New cards
54

Describe the mutex concept:

Problem: Threads expecting to update the most recent copy of shared data can instead read in an old value before another thread that read earlier has had time to update it.

Solution: Surround the problem code with a mutex lock/unlock. When a thread locks the mutex, it can enter that code segment. Other threads trying to enter the portion can’t lock the mutex, so they block outside that portion. The thread that enters does its job, then unlocks the mutex. Now, another thread can lock the mutex, enter the code segment, and do its job. There are no pending writes when an entering thread reads (assuming read/write are contained in the mutex-ed segment).

New cards
55

The unlink C function:

Removes a specified filename, thus decreasing its number of inode links (which usually leads to a remove if the file only had one).

New cards
56

Let’s say your main thread creates a worker thread, which tried to create another thread. The worker thread’s thread creation fails, and on failure, the code called exit(1). Who exits?

Any threads that are part of the overall process.

New cards
57

Describe what each of the following uses of the kill command does:

a. kill 12345 23813

b. kill -9 %1

c. kill -USR1 %3 2213 %5

d. Suppose you know the number of the signal you want to send. How do you send it? What if you know the name of the signal?

a. Sends SIGTERM (15) to processes with pid 12345 23813

b. sends SIGKILL (9) to the process group under job id %1

c. Sends SIGSUR1 to the process group under job ids %3, %5, and pid 2213

d. The kill command’s single option takes either the signal number or signal name (signal constants are of form SIG[NAME]) For example, kill -9 … and kill -KILL … are the same.

New cards
58

Describe these common signals (what are they)?

a. SIGKILL

b. SIGTERM

c. SIGTTIN

d. SIGHUP

e. SIGPIPE

f. SIGSEGV

g. SIGALRM

h. SIGSUR[1/2]

i. SIGINT

j. SIGTSTP

k. SIGSTOP

a. kills the process. Cannot be ignored or handled. The program “emergency kill switch.”

b. Tells process to terminate; process may or may not depending on if it handles the signal.

c. Prevents processes in background process groups from reading terminal input.

d. Tells shell to clean up processes after you disconnect.

e. From writing into a pipe/fifo with no reader. (this cause your program to terminate!)

f. Segmentation fault

g. When the alarm from the alarm function is up.

h. User defined signals that we used in the race programs

i. aka ctrl+c, interrupts, and if unhandled, quits the program

j. ctrl+z, stops the foreground processes unless handled

k. Emergency stop, cannot be ignored or handled

New cards
59

how do you used the signal(…) function to arrange for signals to be handled? (i.e. what are the parameters like, and what is the function it calls like?)

First parameter is signal constant, second is function call to handle, which must take an int argument with void return. (Return is pointer to old function it was calling to handle the signal, but that’s not important).

New cards
60

Our Linux servers reset how a signal is handled after the respective handler is called. If you want the signal to be handled in the same place each time, how should you work around that?

Call the signal function reestablishing the handler, inside the handler itself. (There is a slight issue: if the same signal happens before the handler is reestablished, the program does its default action, which is usually to quit. Not a big deal for this class.)

New cards
61

How do you use the alarm function?

Single parameter is the number of seconds until it generates SIGALARM for you.

New cards
62

Let’s say you press ctrl+c (sending SIGINT), and the program has a handler specified for SIGINT in the sign_int(int signo) function. Track the program execution between signal reception to right after handling the signal.

  1. program stops normal execution

  2. Program enters signal handler

  3. Once handled, resumes in exact location the program left off executing

New cards
63

What data type are signals internally?

They’re ints; serve as IPC before pipes

New cards
64

What header file do you get signals with?

signal.h

New cards
65

What header file do you get threads with?

pthread.h

New cards
66

Issuing two commands with “ | “ connects them with a pipe. What does ; between two commands do?

Lets you execute one, then the next, in sequence

New cards
67

Describe the interactions of the user, the main memory, and the disk for executing processes (APUE 10 pptx slide 5 diagram):

User creates the process, and receives notice of process termination. It’s loaded into main memory and waits for execution. If the CPU is bust, it might get swapped into the disk. When the CPU is ready, the process runs. Perhaps it hits a pipe read while waiting for the other end to write, and becomes blocked. If this takes too long, it may also be swapped into the disk to free main memory for other processes.

New cards
68

How do you use the kill function (not kill command):

First argument is pid to target, and second is signal number (or signal name constant) to send

New cards
69

When you type ctrl+c, the SIGINT signal is directed to:

the foreground process group (i.e. current process running in the foreground and all its children).

New cards
70

What are the Linux S, R, D, T, Z process states?

Where to see these states?

S = interruptible sleep (those that a signal can wake it from)

R = running

D = uninterruptible sleep (disk io)

T = Stopped process (ctrl+Z)

Z = zombie, aka finished child process waiting for parent to get its data

ps -lf (little l) displays

New cards
71

The race/races.c programs were meant to test ________ of the “racing” processes. How could you tell which one “won”?

CPU execution time; the higher counter was probably processed for longer (the “winner”).

New cards
72

What started the race in the race/races.c programs?

What ended the race?

Sending a SIGSUR1 started the race; SIGSUR2 ended the race

New cards
73

When two processes compete for input, what happens?

It’s undefined who gets the input. They might alternate, or one might get more of the input. You don’t know who will receive your input.

New cards
74

After the parent of a foreground process group dies, can its children/ descendants receive io?

No, the shell resumes and prevents the children from getting input (In theory, but very buggy in practice.)

(what really happens is that the shell and parent process are in different process groups. When the parent dies, the shell’s process group comes to the foreground and the OS prevents background process groups from reading io.)

New cards
75

Our mysum with threads used a rudimentary flags system. What was a flaw? How do OS semaphores fix this?

-The waiting while loops wasted CPU time doing nothing

-Semaphores are “OS flags” that can put waiting processes to sleepA

New cards
76

After a fork, the child’s signal handlers are?

After an exec, the signal handlers are?

After creating a thread?

-Inherited from the parent, since it’s guaranteed those handlers still exist.

-Reset, since there is no guarantee old signal handlers exist in the new program.

-Threads share the signal handlers; when one sets it, all use that

New cards
77

Describe how to use stdio.h buffered fread, and what does it return?

Takes: (buffer to read into, size of object, number of objects, FILE*)

returns: num of objects read

New cards
78

Describe how to use unisttd.h read command, and what does it return?

takes: (int fd, buffer, number of bytes to read)

returns: number of bytes

New cards
79

Describe how to stdio.h buffered fwwritte, and what does it return?

takes: (buffer to write from, size of objectt, number of objects, FILE*)

returns: number of objects written

New cards
80

Describe how to use unistd.h write, and what does it return?

takes: (int fd, buffer, number of bytes to write)

returns: number of bytes written

New cards
81

Describe how to use fopen, and what it return?

takes: (filename, use mode string)

returns: FILE* to file, or NULL on error

New cards
82

Describe how to use fclose and what does it return?

takes: (FILE* of file)

returns: 0 on success, EOF on failure

New cards
83

Describe how to use unistd open, and what does it return?

takes: (file name buffer, flags, optional mode [permissions])

returns: int fd, or -1 on error

New cards
84

Describe how to use unistd close, and what does it return?

takes: (int fd)

returns: 0 on success, -1 on error

New cards
85

What should you include to use socket-based programming?

sys/socket.h (sockets themselves)

arpa/inet.h (utilities)

nettinet/in.h (for ip address structures)

New cards
86

Distinguish IP Address and port. Together, (using IPv4), they form a _______. In 102,32,255,12:520, which is the IP address and which is the port?

IP Address: gives an ID/address to a machine (servers, gateways - machines that forward your information, etc) over the internet (or intranet).

Port: A machine may have multiple, allowing it to do many different network services each on a different port.

Together, they form an end point.

New cards
87

Outline the steps needed to set up a server using sockets?

Create socket

Bind socket (may require finding IP address/port)

Listen for clients

Accept a client

Send/ receive messages

Close socket for client when done

New cards
88

Outline the steps needed to set up a client that uses sockets?

Create socket

Connect to server (part of this step may involve getting IP address/port)

Send/ receive messages

Close socket for client when done

New cards
89

Distinguish TCP and UDP.

Which do we use in our socket programs?

UDP: messages are sent without verification they received.

TCP: Three way handshake before any messages are sent to verify the connection. (Also has more steps as a result). We use TCP

New cards
90

How many ports do our Linux servers have?

65,536 of them

New cards
91

Describe each of these:

application layer

transport layer

Internet layer

data link layer

physical layer

-Data in packets used by network applications/services

-information used by TCP/UDP

-Contains IP address and relevant information

-Gets message onto open internet (ethernets, NICs, MAC addresses that service many devices)

-Actual wires

New cards
92

We create sockets using the constant AF_INET because it represents:

socket will use IPv4 protocol (“internet” right now)

New cards
93

The sockaddr_in struct is used as an abstraction to socket addresses (not to be confused with socket fds). These three fields are vital to it being properly interpreted:

1. sin_family

2. sin_addr.s_addr (struct in a struct)

3. sin_port

What should you put in each, and how?

1. We use IPv4, so specify IPv4 using constant AF_INET

2. To get the IP address, assign htonl(INADDR_ANY) - conceptually, INADDR_ANY is the IP address of the current machine.*

3. Port number to use, via htons(port # desired)

htons and htonl convert 16-bit/32-bit integers, respectively, to network byte order. (i.e. standardized form that the internet requires).

*It's a little more complicated in reality. Technically, it's saying assume all the IP addresses the machine owns.

New cards
94

What does memset do, and what do you pass to it?

Takes (buffer address, character, size), and sets size bytes to the character specified starting at the buffer address

New cards
95

How do you use the socket function? What does it return?

(type of IP address (AF_INET for us), type of socket (SOCK_STREAM - the TCP socket for us), protocol (leave 0))

returns socket descriptor (which is just another fd) or -1 for error

New cards
96

How do you use the bind function? What does it return?

(socket fd, pointer to sockaddr (sockaddr_in compatible as well) structure, size of that structure)

Returns 0 normally, -1 on failure.

New cards
97

What are ways bind can fail?

- Sockaddr specifies IP address the machine doesn't have.

- Sockaddr specifies port that is already occupied.

New cards
98

How is listen called? What does it return? What does it do?

Pass it (socket fd, number of requests to queue). Returns 0 on success, -1 on failure. Allows requests to be passed into the socket.

New cards
99

On the client side, how is connect called? What does it return? What does it do?

(socket fd, pointer to sockaddr struct [sockaddr_in is compatible], length of that structure)

0 for success, -1 for failure. Opens connection to the server address specified in the struct. Also binds the client socket to an available address/port.

New cards
100

On the server side, how is accept called? What does it return?

(server's socket fd, sockaddr pointer [if you want client info, otherwise pass NULL], ptr to length of that buffer [NULL for us])

Returns a socket descriptor that allows communication with client.

New cards

Explore top notes

note Note
studied byStudied by 32 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 59 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 21 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 6 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 50 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 22 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 17827 people
Updated ... ago
4.5 Stars(99)

Explore top flashcards

flashcards Flashcard54 terms
studied byStudied by 9 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard34 terms
studied byStudied by 144 people
Updated ... ago
5.0 Stars(4)
flashcards Flashcard120 terms
studied byStudied by 67 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard51 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard34 terms
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard117 terms
studied byStudied by 8 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard46 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard46 terms
studied byStudied by 135 people
Updated ... ago
5.0 Stars(1)