基于Sendmail配置完整功能的邮件服务器----视频下载
2010-03-27 16:41
543 查看
fork
基本知识热身:
每个进程都有自己的ID号码,调度进程的ID为0,init进程的ID为1。fork函数用于创建一个子进程。
1.子进程复制父进程的数据空间,堆,栈
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int glob = 6; //heap
int main(void)
{
int var; //stack
pid_t pid;
var = 88;
printf("before fork\n"); /* we don't flush stdout */
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
else if (pid == 0)
{ /* child */
glob++; /* modify variables */
var++;
}
else
{
sleep(2); /* parent */
}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
exit(0);
}
注明:
1.这段代码在终端的输出如下:
before fork
pid = 430, glob = 7, var = 89
pid = 429, glob = 6, var = 88
可见,子进程改写的是自己的数据空间,并不对父进程的数据空间造成影响
2.如果把这段代码改为文件输出,将会有两行before fork,因为子进程把标准输出缓冲的数据空间也复制了一份,只不过我们连接终端时,标准输出是行缓冲的,换行符洗刷了一次,而连接文件时,标准输出是全缓冲的,所以输出两行
2.文件共享
子进程复制父进程的文件描述符,而和父进程共享文件偏移量
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
char result[3];
//FILE *fp = NULL;
int file_handle;
pid_t pc;
int readnum=0;
//fp = fopen("/home/nsl/myprogram/testfork.txt","rw");
file_handle = open("/home/nsl/myprogram/testfork.txt",O_RDWR);
pc=fork();
if(pc<0)
{
printf("error fork\n");
exit(1);
}
else if(pc>0)
{
sleep(3);
//readnum=fread(result,1,1,fp);
readnum = read(file_handle,result,1);
printf("readnum=%d\n",readnum);
result[1]='\0';
printf("11111111111111=%s\n",result);
exit(0);
}
else if(pc==0)
{
//readnum=fread(result,2,1,fp);
readnum = read(file_handle,result,2);
printf("readnum=%d\n",readnum);
result[2]='\0';
printf("222222222222=%s\n",result);
}
fclose(fp);
close(file_handle);
return 0;
}
注明:
1.父子进程谁先执行,并无定论,随系统而定,一般认为为抢占式
2.如果子进程读了一个字符,那么父进程再读,则是文件的下一个字符,可见他们是共享文件偏移量
3.open是系统调用,fopen是库函数,并且fopen是文件缓冲的,一般认为fopen是open的一个高级封装,打开设备文件,肯定是用open,打开普通文件,可以考虑用fopen,可是这里用fopen只能读到一次,再去读的话,读不出来,具体原因不知道
4.sizeof和strlen的区别:sizeof是个运算符,在程序编译的时出结果,strlen是函数,程序运行的时候出结果,如果对于一个字符串常量什么的,能用sizeof,就用sizeof,因为程序运行的时候会快点本文出自 “nnssll” 博客,谢绝转载!
基本知识热身:
每个进程都有自己的ID号码,调度进程的ID为0,init进程的ID为1。fork函数用于创建一个子进程。
1.子进程复制父进程的数据空间,堆,栈
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int glob = 6; //heap
int main(void)
{
int var; //stack
pid_t pid;
var = 88;
printf("before fork\n"); /* we don't flush stdout */
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
else if (pid == 0)
{ /* child */
glob++; /* modify variables */
var++;
}
else
{
sleep(2); /* parent */
}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
exit(0);
}
注明:
1.这段代码在终端的输出如下:
before fork
pid = 430, glob = 7, var = 89
pid = 429, glob = 6, var = 88
可见,子进程改写的是自己的数据空间,并不对父进程的数据空间造成影响
2.如果把这段代码改为文件输出,将会有两行before fork,因为子进程把标准输出缓冲的数据空间也复制了一份,只不过我们连接终端时,标准输出是行缓冲的,换行符洗刷了一次,而连接文件时,标准输出是全缓冲的,所以输出两行
2.文件共享
子进程复制父进程的文件描述符,而和父进程共享文件偏移量
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
char result[3];
//FILE *fp = NULL;
int file_handle;
pid_t pc;
int readnum=0;
//fp = fopen("/home/nsl/myprogram/testfork.txt","rw");
file_handle = open("/home/nsl/myprogram/testfork.txt",O_RDWR);
pc=fork();
if(pc<0)
{
printf("error fork\n");
exit(1);
}
else if(pc>0)
{
sleep(3);
//readnum=fread(result,1,1,fp);
readnum = read(file_handle,result,1);
printf("readnum=%d\n",readnum);
result[1]='\0';
printf("11111111111111=%s\n",result);
exit(0);
}
else if(pc==0)
{
//readnum=fread(result,2,1,fp);
readnum = read(file_handle,result,2);
printf("readnum=%d\n",readnum);
result[2]='\0';
printf("222222222222=%s\n",result);
}
fclose(fp);
close(file_handle);
return 0;
}
注明:
1.父子进程谁先执行,并无定论,随系统而定,一般认为为抢占式
2.如果子进程读了一个字符,那么父进程再读,则是文件的下一个字符,可见他们是共享文件偏移量
3.open是系统调用,fopen是库函数,并且fopen是文件缓冲的,一般认为fopen是open的一个高级封装,打开设备文件,肯定是用open,打开普通文件,可以考虑用fopen,可是这里用fopen只能读到一次,再去读的话,读不出来,具体原因不知道
4.sizeof和strlen的区别:sizeof是个运算符,在程序编译的时出结果,strlen是函数,程序运行的时候出结果,如果对于一个字符串常量什么的,能用sizeof,就用sizeof,因为程序运行的时候会快点本文出自 “nnssll” 博客,谢绝转载!
相关文章推荐
- 基于Java web技术文件上传和下载功能开发实战练习视频(fileupload)
- mybatise基于xml配置的实现增删改查的一个完整项目下载
- ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)
- 基于开源项目搭建完整RTMP视频直播(基础入门)
- Linux 音视频技术] 转:ffmpeg使用x264编码的配置+ ffmpeg与 x264编码器参数完整对照表
- Python视频爬虫实现下载头条视频功能示例
- RHEL4上安装基于postfix的全功能邮件服务器
- centos sendmail 邮件服务器配置
- Ubuntu Server 12.04 配置邮件服务器完整篇
- IIS安装详细的操作步骤;IIS下载地址;IIS安装和视频配置教程;系统和IIS 版本的匹配
- struts配置上传、下载功能
- (3) [Mail]RHEL4上安装基于postfix的全功能邮件服务器
- (5) [Mail]RHEL4上安装基于postfix的全功能邮件服务器
- 在centos下用sendmail简单配置带smtp邮件服务器
- asp.net mvc5 使用百度ueditor 本编辑器完整示例(下)配置上传播放视频
- sendmail +apache+openwebmail邮件服务器安装配置
- Sendmail邮件服务器搭建与配置笔记
- 带SMTP认证功能的sendmail配置 sasl库
- 趁热打铁之---VS2010基于VFW的视频控件编写,实现预览、拍照功能
- Android程序自动更新功能模块的实现方法【附完整demo源码下载】