Skip to content

Commit 1aefa84

Browse files
authored
Add files via upload
1 parent 45280d7 commit 1aefa84

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

final_project_part1.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
// Read an data frm stadard input device (keyboard)
3+
// Write it on the screen
4+
// Data should be any length
5+
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
int main(){
11+
12+
char data[2000]; //Initialising the string. However an infinte
13+
//string is not possible so we use a large buffer
14+
//number instead
15+
16+
printf("Enter anything:\n");
17+
18+
scanf("%s", data); //Takes input from user
19+
printf("Output -> %s ", data); //Prints said input
20+
return 0;
21+
}

final_project_part2.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// print day of the week
2+
// fork to print date
3+
4+
#include <stdio.h>
5+
#include <unistd.h>
6+
#include <time.h>
7+
8+
int main(int argc, char *argv[])
9+
{
10+
time_t systemCurrentTime;
11+
time(&systemCurrentTime); //get the date and time
12+
13+
//Parent process
14+
if(fork() == 0)
15+
printf("Day: %.3s\n", ctime(&systemCurrentTime));
16+
17+
//Child process
18+
else
19+
{
20+
printf("Date : %.*s ", 6, ctime(&systemCurrentTime) + 4);
21+
printf("%.*s\n", 4, ctime(&systemCurrentTime) + 20);
22+
}
23+
24+
return 0;
25+
}

final_project_part3/clientB

49 KB
Binary file not shown.

final_project_part3/clientB.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <netdb.h>
9+
#include <arpa/inet.h>
10+
void error(const char *msg)
11+
{
12+
perror(msg);
13+
exit(0);
14+
}
15+
16+
int main(int argc, char *argv[])
17+
{
18+
int sockfd, portno = 9999;
19+
struct sockaddr_in serv_addr;
20+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
21+
if (sockfd < 0)
22+
error("ERROR opening socket");
23+
bzero(&serv_addr, sizeof(serv_addr));
24+
serv_addr.sin_family = AF_INET;
25+
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
26+
serv_addr.sin_port = htons(portno);
27+
28+
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
29+
{
30+
printf("Client sending 'This is client B' to server\n");
31+
char buf[1024];
32+
strncpy(buf, "Recieved client B", 20);
33+
write(sockfd, buf, strlen(buf));
34+
int count = read(sockfd, buf, 1024);
35+
printf("Got echo of %s from server\n", buf);
36+
shutdown(sockfd, SHUT_RDWR);
37+
}
38+
else
39+
error("ERROR connecting");
40+
return 0;
41+
}
42+
43+

final_project_part3/clientC

49 KB
Binary file not shown.

final_project_part3/clientC.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <netdb.h>
9+
#include <arpa/inet.h>
10+
void error(const char *msg)
11+
{
12+
perror(msg);
13+
exit(0);
14+
}
15+
16+
int main(int argc, char *argv[])
17+
{
18+
int sockfd, portno = 9999;
19+
struct sockaddr_in serv_addr;
20+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
21+
if (sockfd < 0)
22+
error("ERROR opening socket");
23+
bzero(&serv_addr, sizeof(serv_addr));
24+
serv_addr.sin_family = AF_INET;
25+
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
26+
serv_addr.sin_port = htons(portno);
27+
28+
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
29+
{
30+
printf("Client sending 'This is client C' to server\n");
31+
char buf[1024];
32+
strncpy(buf, "Recieved client C", 20);
33+
write(sockfd, buf, strlen(buf));
34+
int count = read(sockfd, buf, 1024);
35+
printf("Got echo of %s from server\n", buf);
36+
shutdown(sockfd, SHUT_RDWR);
37+
}
38+
else
39+
error("ERROR connecting");
40+
return 0;
41+
}
42+
43+

final_project_part3/server.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <pthread.h>
9+
#include <arpa/inet.h>
10+
11+
void error(const char *msg)
12+
{
13+
perror(msg);
14+
exit(1);
15+
}
16+
17+
void* echo(void* param)
18+
{
19+
char buf[1024];
20+
int count;
21+
pthread_detach(pthread_self());
22+
int s = (int)param;
23+
while( (count = read(s, buf, 1023)) > 0 ) {
24+
printf("Hello client B%s\n", buf);
25+
printf("Server sending it back\n");
26+
write(s, buf, strlen(buf));
27+
}
28+
29+
close(s);
30+
}
31+
32+
int main(int argc, char *argv[])
33+
{
34+
int sockfd, newsockfd, portno = 9999;
35+
// create a TCP/IP socket
36+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
37+
if (sockfd < 0)
38+
error("ERROR opening socket");
39+
40+
struct sockaddr_in serv_addr;
41+
// clear address structure
42+
bzero((char *) &serv_addr, sizeof(serv_addr));
43+
44+
/* setup the host_addr structure for use in bind call */
45+
// server byte order
46+
serv_addr.sin_family = AF_INET;
47+
48+
// automatically be filled with current host's IP address
49+
serv_addr.sin_addr.s_addr = INADDR_ANY;
50+
51+
// port number to bind to
52+
serv_addr.sin_port = htons(portno);
53+
54+
// This bind() call will bind the socket to the current IP address on port
55+
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
56+
{
57+
error("ERROR on binding");
58+
}
59+
60+
// This listen() call tells the socket to listen to the incoming connections.
61+
// The listen() function places all incoming connection into a backlog queue
62+
// until accept() call accepts the connection.
63+
// Here, we set the maximum size for the backlog queue to 5.
64+
listen(sockfd,5);
65+
66+
while(newsockfd == accept(sockfd, 0, 0)) {
67+
pthread_t t;
68+
pthread_create(&t, 0, echo, (void*)newsockfd);
69+
}
70+
71+
return 0;
72+
}
73+

final_project_part4.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
int MAX = 14;
7+
int thr1V = 0;
8+
int thr2V = 0;
9+
int count = 1;
10+
int thr1C = 1;
11+
int thr2C = 2;
12+
pthread_mutex_t thr;
13+
pthread_cond_t cond;
14+
15+
//Even numbers so from 2 to 14 but in two's
16+
void *even(void *arg){
17+
while(count < MAX) { // to make sure we don't pass 14
18+
pthread_mutex_lock(&thr);
19+
while(count % 2 != 0)
20+
pthread_cond_wait(&cond, &thr);
21+
if (count != MAX)
22+
printf("%d -> ", thr2V + thr2C);
23+
else
24+
printf("%d\n ", thr2V + thr2C);
25+
thr2V = thr2V + thr2C;
26+
count++;
27+
pthread_mutex_unlock(&thr);
28+
pthread_cond_signal(&cond);
29+
}
30+
pthread_exit(0);
31+
}
32+
33+
//Prints all values from 1 to 14 a
34+
void *all(void *arg){
35+
while(count < MAX) {
36+
pthread_mutex_lock(&thr);
37+
while(count % 2 != 1)
38+
pthread_cond_wait(&cond, &thr);
39+
printf("%d -> ", thr1C + thr1V);
40+
thr1V = thr1V + thr1C;
41+
count++;
42+
pthread_mutex_unlock(&thr);
43+
pthread_cond_signal(&cond);
44+
}
45+
pthread_exit(0);
46+
}
47+
48+
//Main Function
49+
int main(){
50+
pthread_t thread1;
51+
pthread_t thread2;
52+
pthread_mutex_init(&thr, 0);
53+
pthread_cond_init(&cond, 0);
54+
pthread_create(&thread1, 0, &even, NULL);
55+
pthread_create(&thread2, 0, &all, NULL);
56+
pthread_join(thread1, 0);
57+
pthread_join(thread2, 0);
58+
pthread_mutex_destroy(&thr);
59+
pthread_cond_destroy(&cond);
60+
return 0;
61+
}

0 commit comments

Comments
 (0)