0
0
Operating Systemsknowledge~10 mins

Inter-process communication (pipes, shared memory) in Operating Systems - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a pipe for communication between processes.

Operating Systems
int pipefd[2];
if (pipe([1]) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
}
Drag options to blanks, or click blank then click option'
Apipefd_array
Bfd
Cfd_pipe
Dpipefd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single integer instead of an array.
Passing an uninitialized pointer instead of an array.
2fill in blank
medium

Complete the code to write data to the write end of the pipe.

Operating Systems
char buf[] = "Hello";
write([1], buf, sizeof(buf));
Drag options to blanks, or click blank then click option'
Afd[0]
Bpipefd[0]
Cpipefd[1]
Dfd[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Writing to the read end of the pipe.
Confusing the indices of the pipe file descriptors.
3fill in blank
hard

Fix the error in the shared memory segment creation code.

Operating Systems
int shmid = shmget(key, [1], IPC_CREAT | 0666);
if (shmid < 0) {
    perror("shmget");
    exit(1);
}
Drag options to blanks, or click blank then click option'
A0
B1024
Csizeof(int)
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as the size, which causes failure.
Using negative values or invalid sizes.
4fill in blank
hard

Fill both blanks to attach to a shared memory segment and write a value.

Operating Systems
char *shm_ptr = (char *) shmat(shmid, [1], [2]);
if (shm_ptr == (char *) -1) {
    perror("shmat");
    exit(1);
}
*shm_ptr = 'A';
Drag options to blanks, or click blank then click option'
ANULL
B0
CSHM_RDONLY
DIPC_CREAT
Attempts:
3 left
💡 Hint
Common Mistakes
Using SHM_RDONLY when intending to write.
Passing IPC_CREAT to shmat, which is invalid.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps process IDs to their shared memory keys if the key is positive.

Operating Systems
shm_dict = { [1]: [2] for pid, key in processes.items() if key [3] 0 }
Drag options to blanks, or click blank then click option'
Apid
Bkey
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values in the dictionary.
Using the wrong comparison operator in the condition.