Skip to content

Commit f09037a

Browse files
committed
Adding implementations for shared memory server client, zombie proccesses, and more
1 parent 9b25de7 commit f09037a

File tree

7 files changed

+261
-0
lines changed

7 files changed

+261
-0
lines changed

apocalypse.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<stdio.h>
2+
#include<unistd.h>
3+
4+
int
5+
main()
6+
{
7+
int flag = 0;
8+
9+
while (fork() > 0)
10+
{
11+
flag++;
12+
printf("%d\n", flag);
13+
}
14+
15+
return 0;
16+
}

orphan.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <unistd.h>
2+
3+
int
4+
main()
5+
{
6+
pid_t pid = fork();
7+
8+
if (pid == 0)
9+
{
10+
sleep(30);
11+
}
12+
13+
return 0;
14+
}

portscanner.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import socket
2+
3+
target = str(input("Enter ip of target to scan available ports, empty for localhost: "))
4+
5+
for port in range(1,65535):
6+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7+
socket.setdefaulttimeout(0.1)
8+
9+
if not target:
10+
target = "127.0.0.1"
11+
12+
result = s.connect_ex((target,port))
13+
if result == 0:
14+
print("Port " + str(port) + " is open")
15+
s.close()

shared_memory/shm_reader.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<errno.h>
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
#include<string.h>
5+
#include<sys/ipc.h>
6+
#include<sys/shm.h>
7+
#include<sys/types.h>
8+
#include<unistd.h>
9+
10+
#define BUF_SIZE 1024
11+
#define SHM_KEY 0x568547
12+
13+
struct shmsegment
14+
{
15+
int cnt;
16+
int opercompleted;
17+
char buffer[BUF_SIZE];
18+
};
19+
20+
int
21+
main(int argc, char *argv[])
22+
{
23+
int shmid;
24+
struct shmsegment *shmp;
25+
26+
shmid = shmget(SHM_KEY, sizeof(struct shmsegment), 0644 | IPC_CREAT);
27+
if (shmid == -1)
28+
{
29+
perror("Error getting the shared memory segment");
30+
return 1;
31+
}
32+
33+
// Attach to shared memory segment to get a pointer to it
34+
shmp = shmat(shmid, NULL, 0);
35+
if (shmp == (void *) -1)
36+
{
37+
perror("Error attaching to shared memory");
38+
return 1;
39+
}
40+
41+
// Transfer data from shared memory segment to standard output
42+
while (shmp->opercompleted != 1)
43+
{
44+
printf("Memory segment contains : \n\"%s\"\n", shmp->buffer);
45+
if (shmp->cnt == -1)
46+
{
47+
perror("Error reading shared memory segment");
48+
return 1;
49+
}
50+
51+
printf("RP: Read %d bytes from shared memory\n", shmp->cnt);
52+
sleep(3);
53+
}
54+
55+
printf("Reading process has been completed\n");
56+
printf("Detaching from shared memory\n");
57+
58+
if (shmdt(shmp) == -1)
59+
{
60+
perror("Unable to detach from shared memory");
61+
return 1;
62+
}
63+
64+
printf("Reading process has been completed\n");
65+
return 0;
66+
}

shared_memory/shm_writer.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include<errno.h>
2+
#include<stdlib.h>
3+
#include<stdio.h>
4+
#include<string.h>
5+
#include<unistd.h>
6+
#include<sys/ipc.h>
7+
#include<sys/shm.h>
8+
#include<sys/types.h>
9+
10+
#define BUF_SIZE 1024
11+
#define SHM_KEY 0x568547
12+
13+
struct shmsegment
14+
{
15+
int cnt;
16+
int opercompleted;
17+
char buffer[BUF_SIZE];
18+
};
19+
20+
int fill_buffer(char * buffptr, int size);
21+
22+
int
23+
main(int argc, char *argv[])
24+
{
25+
int shmid, times;
26+
int availspace;
27+
struct shmsegment *shmp;
28+
char *buffptr;
29+
30+
shmid = shmget(SHM_KEY, sizeof(struct shmsegment), 0644 | IPC_CREAT);
31+
if (shmid == -1)
32+
{
33+
perror("Error creating shared memory");
34+
return 1;
35+
}
36+
37+
// Attach to shared memory segment to get a pointer to it
38+
shmp = shmat(shmid, NULL, 0);
39+
if (shmp == (void *) -1)
40+
{
41+
perror("Error attaching to shared memory");
42+
return 1;
43+
}
44+
45+
// Transfer data from buffer to shared memory
46+
buffptr = shmp->buffer;
47+
availspace = BUF_SIZE;
48+
49+
for (times = 0; times < 5; times++)
50+
{
51+
shmp->cnt = fill_buffer(buffptr, availspace);
52+
shmp->opercompleted = 0;
53+
54+
printf("WP: Wrote %d bytes in shared memory\n", shmp->cnt);
55+
buffptr = shmp->buffer;
56+
availspace = BUF_SIZE;
57+
58+
sleep(3);
59+
}
60+
61+
printf("WP: Wrote %d times\n", times);
62+
shmp->opercompleted = 1;
63+
64+
if (shmdt(shmp) == -1)
65+
{
66+
perror("Unable to detach from shared memory");
67+
return 1;
68+
}
69+
70+
if (shmctl(shmid, IPC_RMID, 0) == -1)
71+
{
72+
perror("Unable to perform shared memory control operations");
73+
return 1;
74+
}
75+
76+
printf("Writting process has been completed\n");
77+
return 0;
78+
}
79+
80+
int
81+
fill_buffer(char* buffptr, int size)
82+
{
83+
static char c = 'K';
84+
int fillcount;
85+
86+
memset(buffptr, c, size - 1);
87+
buffptr[size-1] = '\0';
88+
89+
if (c > 122)
90+
{
91+
c = 65;
92+
}
93+
94+
if ( (c >= 65) && (c <= 122) )
95+
{
96+
if ( (c >= 91) && (c <= 96) )
97+
{
98+
c = 65;
99+
}
100+
}
101+
102+
fillcount = strlen(buffptr);
103+
c++;
104+
105+
return fillcount;
106+
}

variadic_template.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
void
4+
print()
5+
{
6+
std::cout << "I am called last and i am empty.\n";
7+
}
8+
9+
template <typename T, typename... Types>
10+
void
11+
print(T arg1, Types... arg2)
12+
{
13+
std::cout << arg1 << std::endl;
14+
15+
print(arg2...);
16+
}
17+
18+
int main()
19+
{
20+
print(1, 5020345, 0.5772156649,
21+
"I will print any and all arguments",
22+
"The defrosting has begun\n");
23+
24+
return 0;
25+
}

zombie.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
#include <unistd.h>
3+
4+
int
5+
main()
6+
{
7+
pid_t child_pid = fork();
8+
9+
if (child_pid > 0)
10+
{
11+
sleep(50);
12+
}
13+
else
14+
{
15+
exit(0);
16+
}
17+
18+
return 0;
19+
}

0 commit comments

Comments
 (0)