int main(){
int res=fork();
int i=0;
if(res >)){
exit(0);
}
setsid();
chdir("/");
umask(0);
close(0);
close(1);
colse(3);
}
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int Create_Daemon()
{
int pid;
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGHUP ,SIG_IGN);
umask(0);
pid = fork();
if(pid < 0 )
{
printf("error fork");
return -1;
}
else if(pid > 0)
{
exit(0);
}
setsid();
signal(SIGCHLD,SIG_IGN);
if(pid = fork())
{
exit(0);
}
else if(pid <0)
{
perror("fork");
exit(-1);
}
close(0);close(1);close(2);
chdir("/");
return 0;
}
int main()
{
Create_Daemon();
while(1);
return 0;
}
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
#include<stdlib.h>
int ThreadVal=0;
sem_t sem1,sem2;
void *fun1(void *arg){
while(1){
sem_wait(&sem1);
sleep(1);
if(ThreadVal<0){
sem_post(&sem2);
pthread_exit(NULL);
}
ThreadVal++;
printf("+++++thread1 ThreadVal=%d\n",ThreadVal);
sem_post(&sem2);
}
}
void *fun2(void *arg){
while(1){
sem_wait(&sem2);
sleep(1);
if(ThreadVal<0){
sem_post(&sem1);
pthread_exit(NULL);
}
ThreadVal-=2;
printf("-----thread2 ThreadVal=%d\n",ThreadVal);
sem_post(&sem1);
}
}
int main(int argc,char *argv[]){
if(argc<2){
return -1;
}
ThreadVal=atoi(argv[1]);
pthread_t thread1,thread2;
int ret=0;
ret=sem_init(&sem1,0,1);
ret=sem_init(&sem2,0,0);
ret=pthread_create(&thread1,NULL,fun1,NULL);
ret=pthread_create(&thread2,NULL,fun2,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
int main(){
pid_t pid;
pid=fork();
if(pid==0){
printf("i am a child\n");
}else{
sleep(1);
printf("i am father\n");
}
return 0;
}
#include<stdio.h>
#include<pthread.h>
int a=0;
int b=0;
pthread_mutex_t mutex;
void *fun1(void *arg){
while(1){
pthread_mutex_lock(&mutex);
a++;
b++;
if(a!=b){
printf("%d!=%d\n",a,b);
a=b=0;
}else{
printf("%d==%d\n",a,b);
}
pthread_mutex_unlock(&mutex);
}
}
void *fun2(void *arg){
while(1){
pthread_mutex_lock(&mutex);
a++;
b++;
if(a!=b){
printf("%d!=%d",a,b);
a=b=0;
}
pthread_mutex_unlock(&mutex);
}
}
int main(){
pthread_t thread1,thread2;
pthread_mutex_init(&mutex,NULL);
pthread_create(&thread1,NULL,fun1,NULL);
pthread_create(&thread2,NULL,fun2,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
#include <stdio.h>
#include<string.h>
int main()
{
FILE *f;
f=fopen("file1.txt","r");
char buf1[20]="";
int count=0;
while(fgets(buf1,20,f)){
if(buf1[strlen(buf1)-1]=='\n'){
count++;
}
}
printf("%d\n",count);
close(f);
return 0;
}
reader.c 从argv[1]所指定的文件中读取内容,依次写到管道/home/linux/myfifo中
writer.c 从管道/home/linux/myfifo中读取内容,写到argv[1]所指定的文件中并保存
代码中可省略头文件,/home/linux/myfifo无需创建
#include<stdio.h>
int main(int argc,char *argv[]){
int fd,fd_scr;
char buf[32];
int bytes;
if(argc<2){
return -1;
}
fd=open("/home/linux/myfifo",O_WRONLY);
if(fd<0){
return -1;
}
fd_src=open(argv[1],O_RDONLY);
if(fd_src<0){
return -1;
}
while((bytes=read(fd_src,buf,32))>0){
write(fd,buf,bytes);
}
close(fd);
close(fd_src);
return 0;
}
int main(int argc,char *argv[]){
int fd,fd_dest;
char buf[32];
int bytes;
if(argc<2){
return -1;
}
fd=open("/home/linux/myfifo",O_RDONLY);
if(fd<0){
return -1;
}
fd_dest=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0666);
if(fd_src<0){
return -1;
}
while((bytes=read(fd_src,buf,32))>0){
write(fd_dest,buf,bytes);
}
close(fd);
close(fd_dest);
return 0;
}
|