1、父进程创建一个新的文本文件,并向文本文件中写入数据,当数据写入完成后,用匿名管道通信的方式将该文件传输给子进程,同时用命名管道文件将该文件传输给另一个进程
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#define BUFSZ 256
#define FIFO_NAME "/home/xianfeng/my_fifo"
#define BUFFER_SIZE PIPE_BUF
int main(void)
{
FILE *fp;
char *hello="hello ";
int res;
int fd[2];
char buf[BUFSZ];
pid_t pid;
int len;
if( pipe(fd)<0 )
{
perror("管道调用失败");
exit(1);
}
if( (pid = fork())<0 )
{
perror("创建进程失败");
exit(1);
}
else if(pid > 0)
{
printf("这是父进程\n");
//fd[1]=open("test,txt");
printf("父进程写入hello");
write(fd[1], "hello\n", 25);
printf("\n");
if(access(FIFO_NAME,F_OK)==-1){
res=mkfifo(FIFO_NAME,0777);
if(res!=0){
fprintf(stderr,"创建fifo文件失败\n");
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO\n", getpid());
res = open(FIFO_NAME, O_WRONLY);
printf("Process %d result %d\n", getpid(), res);
sleep(5);
if (res != -1){
write(res,"Hello Linux!\n",25);
}
(void)close(res);
printf("Process %d finished\n", getpid());
exit(EXIT_SUCCESS);
}
else
{
printf("这是子进程\n");
read(fd[0], buf, BUFSZ);
printf("子进程读出结果 %s\n", buf);
}
return 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/home/xianfeng/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 * 10)
int main()
{
int pipe_fd;
int res;
int open_mode = O_RDONLY;
int bytes_sent = 0;
char buffer[BUFFER_SIZE + 1];
printf("Process %d opening FIFO O_RDONLY\n", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
read(pipe_fd,buffer,25);
printf("%s\n",buffer);
printf("Process %d finished\n", getpid());
exit(EXIT_SUCCESS);
}
2、利用套接字分别编写一个简易的服务器端程序和客户端程序,实现基于INTERNET的双向网络通信
/*client.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch = 'A';
sockfd = socket(AF_INET, SOCK_STREAM, 0); //创建套接字
//套接字命名
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = 9734;
len = sizeof(address);
//连接到服务器套接字
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1) {
perror("oops: client");
exit(1);
}
//通信操作
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %c\n", ch);
close(sockfd);
exit(0);
}
/*server.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//创建套接字
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
//命名套接字
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = 9734;
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len); //bind命名
//创建一个连接队列
listen(server_sockfd, 5); //创建容量为5的队列
while(1) {
char ch;
printf("server waiting\n");
//接受一个连接
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len); //accept接受客户连接
//通信操作
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
3、假设有一个整数初值为200,开20个线程,每个线程将这个整数减掉10,最后刚好把这个整数减到0,;要求考虑同步与互斥管理,实现死锁的检测和避免管理
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_THREADS 20
void *thread_function(void *arg);
sem_t bin_sem;
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int num=200;
int main() {
int res;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
int lots_of_threads;
res = sem_init(&bin_sem, 0, 0);
if (res != 0) {
perror("信号初始化错误\n");
exit(EXIT_FAILURE);
}
for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads);//创建进程,用数组表示
if (res != 0) {
perror("线程创建失败\n");
exit(EXIT_FAILURE);
}
sleep(1);
}
for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
res = pthread_join(a_thread[lots_of_threads], &thread_result/*指向线程返回值*/); //以阻塞的方式等待线程结束,进程同步操作
if (res == 0) {
printf("Picked up a thread\n");
}
else {
perror("pthread_join failed");
}
}
printf("All done\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) {
sem_post(&bin_sem);
int my_number = *(int *)arg;
int rand_num;
num-=10;
printf("%d\n",num);
printf("thread_function is running. Argument was %d\n", my_number);
rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
sleep(rand_num);
printf("Bye from %d\n", my_number);
sem_post(&bin_sem);
pthread_exit(NULL);
}