feat: 2106 misc
This commit is contained in:
32
labs/cs2106/lectures/l5/1.c
Normal file
32
labs/cs2106/lectures/l5/1.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int shmid, *shm;
|
||||||
|
|
||||||
|
shmid = shmget(IPC_PRIVATE, 4, IPC_CREAT | 0600);
|
||||||
|
|
||||||
|
if (shmid == -1) {
|
||||||
|
printf("Cannot create shared memory!\n");
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
printf("Shared memory ID = %d\n", shmid);
|
||||||
|
}
|
||||||
|
shm = (int*) shmat(shmid, NULL, 0);
|
||||||
|
if (shm == (int*) -1) {
|
||||||
|
printf("Cannot attach shared memory!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
shm[0] = 0;
|
||||||
|
while(shm[0] == 0) {
|
||||||
|
sleep(3);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
printf("Read %d from shared memory. \n", shm[i+1]);
|
||||||
|
}
|
||||||
|
shmdt((char*) shm);
|
||||||
|
shmctl(shmid, IPC_RMID, 0);
|
||||||
|
}
|
||||||
|
|
||||||
26
labs/cs2106/lectures/l5/2.c
Normal file
26
labs/cs2106/lectures/l5/2.c
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int shmid, input, *shm;
|
||||||
|
printf("Shared memory id: ");
|
||||||
|
scanf("%d", &shmid);
|
||||||
|
|
||||||
|
shm = (int*) shmat(shmid, NULL, 0);
|
||||||
|
if (shm == (int*) -1) {
|
||||||
|
printf("Error: Cannot attach!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
scanf("%d", &input);
|
||||||
|
shm[i+1] = input;
|
||||||
|
}
|
||||||
|
shm[0] = 1;
|
||||||
|
|
||||||
|
shmdt((char*) shm);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
24
labs/cs2106/lectures/l5/3.c
Normal file
24
labs/cs2106/lectures/l5/3.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#define READ_END 0
|
||||||
|
#define WRITE_END 1
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int pipeFd[2], pid, len;
|
||||||
|
|
||||||
|
char buf[100], *str = "Hello There!";
|
||||||
|
|
||||||
|
pipe(pipeFd);
|
||||||
|
|
||||||
|
if ((pid = fork()) > 0) {
|
||||||
|
close(pipeFd[READ_END]);
|
||||||
|
write(pipeFd[WRITE_END], str, strlen(str) +1);
|
||||||
|
close(pipeFd[WRITE_END]);
|
||||||
|
} else {
|
||||||
|
close(pipeFd[WRITE_END]);
|
||||||
|
len = read(pipeFd[READ_END], buf, sizeof(buf));
|
||||||
|
printf("Proc %d read: %s\n", pid, buf);
|
||||||
|
close(pipeFd[READ_END]);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
labs/cs2106/lectures/l5/4.c
Normal file
21
labs/cs2106/lectures/l5/4.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void myOwnHandler(int signo) {
|
||||||
|
if (signo == SIGSEGV) {
|
||||||
|
printf("Memory Access blows up!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int *ip = NULL;
|
||||||
|
|
||||||
|
if (signal(SIGSEGV, myOwnHandler) == SIG_ERR)
|
||||||
|
printf("Failed to register handler\n");
|
||||||
|
|
||||||
|
*ip = 123;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user