Unix Flashcards
1
Q
opendir(), readdir, closedir
A
struct dirent {
inot_t d_no;
char d_name[NAM_MAX+1];
}
2
Q
stat
A
struct stat {
mode_t st_mode;
ino_t st_ino;
}
3
Q
Pipes alternatives
A
- Original pipes (linux unnamed pipes) are a common form of Unix System IPC
- but they are limited:
- half-duplex
- can be used only by children of the same process
- last only as long as the process
- can be seen as unstuctured sequential files
- Alternatives are:
- pipes extension (popen pclose)
- FIFO
- Message queues
- Shared memory: like memory-mapped files (will see them in windows)
4
Q
FIFO for Interprocess communication
A
include <sys></sys>
- FIFOs are extensions of traditional pipes aka named pipes
- Process:
- A FIFO special file is entered into the file system by calling mkfifo
- once created any porcess can open it for reading/writing as it was a file
- a FIFO file must be open at both ends to do any input output on it.
int mkfifo(const char *path, mode_t mode);
int mkfifoat(int fd, const char *path, mode_t mode);
- returns 0, if successul
- -1 on error
- mode is equivalent to the open sys.call -> S_[RWX]USR
- once FIFO is there, we can use: open, read, write, close;
5
Q
Message queus for interprocess communication
A
- created or opened (given that it exists already) by msgget
- int msgget(key_t key, int flag);
- flag is used to define permission field to ds associated with message queue
- returns msqid if success otherwise -1
- the queue may be controlled by msgctl
- int msgctl(int msqid, int cmd, struc msqid_ds *buf);
- commands:
- IPC_STAT: store msqid_ds associated to mq in buf
- IPC_SET: copies fields from buf to mqid_ds associated to mq
- IPC_RMID: remove the message queue from the system
-
msgsnd: adds messages at the end of the queue
- specific format: non negative length + actual data bytes
struct { long int mtype; char mtext[512]; } - int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag);
- ptr points to message data structure
- flag is either 0 or IPC_NOWAIT: if message queue is full and IPC_NOWAIT is parameter we get EAGAIN as return value.
- specific format: non negative length + actual data bytes
-
msgrcv: fetches messages from the queue
- doesn’t have to be in-order
- could be fetched by type
- ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag);
- similar to msgsnd
- type let’s us specify which message we want:
- type = 0: first message in the queue
- type > 0: first message on the queue whose type equals returned type
- type < 0: first message with lowest value that is <= to the absolute value of the message
- return value = size of data portion of the message
- doesn’t have to be in-order
6
Q
ftok
A
include <sys></sys>
- One way to generate and share identifiers and keys between processes
- whenever an IPC structure is created a key must be specified, that key is of type key_t -> the kernel can convert this key in an identifier, that is used as internal reference.
- ftok is used to generate the same key accross different processes
- ftok does not provide means of communication, just generates the same key given the same input.
key_t ftok(const char *path, int id);
path -> file must exists
id -> project id (a character between 0 and 255);
7
Q
Shared memory
A
Shared memory allows two or more processes to share a given region of memory, requires synchronization.
Process logic:
- ftok: generate unique key to manage shared memory process
- shmget: returns identifier for shared segment
- int shmget(key_t key, size_t, int flag)
- size: bytes we want in the segment
- flag: mode of the field
- returns shared memory id on success, -1 on error
- shmat: attach the user process address space to the shared memory segment
- void *shmat(int shmid, const void *addr, int flag);
- the address in the calling process at which the segment is attached depends on addr and on wheter the flag is set to SHM_RND
- returns the pointer to the shared memory on success, -1 otherwise
- shmdt: detach the process with the shared segment a the end of the process
- int shmdt(const void *addr)
- this doesn’t remove the identifier and the associated data structure from the system
- shmctl: destroy shared memory segment after detaching
- int shmctl(int shmid, int cmd, struct shmid_ds *buf)
- shmid: value returned by shmget
- cmd: similar to msgctl (PC_STAT, IPC_SET, IPC_RMID)
- returns 0 on ok, -1 on error
- the shared memory identifier its deleted by calling IPC_RMID