}
else {
wait_mark=1; signal(16,stop); waiting( ); lockf(stdout,1,0);
printf(―child process1 is killed by parent!\\n‖); lockf(stdout,0,0);
exit(0); } }
void waiting()
{while(wait_mark!=0);} void stop( )
{ wait_mark=0;}
2) 在上面的程序中增加语句signal(SIGINT,SIG-IGN)和 signal(SIGQUIT,SIG-IGN),观察执行结果,并分析原因。
参考程序:
#include
void IntDelete() {
kill(pid1,16); kill(pid2,17); EndFlag=1; }
void Int1() {
printf(―child process1 is killed! By parent\\n‖); exit(0); }
Void Int2( ) {
printf(―child process2 is killed! By parent\\n‖); exit(0); }
36
main() {
int exitpid;
signal(SIGINT,SIG_IGN); signal(SIGQUIT,SIG_IGN);
while((pid1=fork())==-1); if(pid1==0)
{
signal(SIGUSR1,Int1);
signal(SIGINT,SIG_IGN);
pause( ); exit(0); } else {
while((pid2=fork())==-1); if(pid2==0)
{
signal(SIGUSR1,Int1);
signal(SIGINT,SIG_IGN); pause( ); exit(0);
} else
{
signal(SIGINT,IntDelete); waitpid(-1,&exitcode,0);
printf(―parent process is killed!\\n‖); exit(0); }
} }
分析:由于忽略了中断与退出信号,程序会一直保持阻塞状态而无法退出。
实验三 存储管理
3、实验内容
(1)编写程序:申请内存、使用内存及释放一块内存。
参考程序:
#include
{
37
char *str;
/*为字符串申请分配一块内存*/
if((str=(char *)malloc(10))==NULL)
{printf(―Not enough memory to allocate buffer\\n‖); exit(1); /*若失败则结束程序*/ } /*拷贝字符串“Hello”到已分配的内存空间*/ Strcpy(str,“Hello”); /*显示该字符串*/ printf(―string is %s\\n‖,str); /*内存使用完毕,释放*/ free(str); return 0; }
运行结果:
String is Hello
(2)在上述实验的基础上重新分配内存。 参考程序:
#include
{
char *str;
/*为字符串申请分配一块内存*/
if((str=(char *)malloc(10))==NULL)
{printf(―Not enough memory to allocate buffer\\n‖); exit(1); /*若失败则结束程序*/ }
/*拷贝字符串“Hello”到已分配的内存空间*/ Strcpy(str,“Hello”);
/*显示该字符串和其所在的地址*/ printf(―string is %s\\n Address is %p\\n‖,str,str);
/*重新分配刚才申请到的内存空间,使空间增大一倍*/ if((str=(char*)realloc(str,20))==NULL) {printf(―Not enough memory to allocate buffer\\n‖); exit(1); /*若失败则结束程序*/ }
/*显示重新分配后的地址*/
printf(‖String is %s\\n New address is %p/n‖,str,str); /*内存使用完毕,释放*/ free(str); return 0; }
运行结果:
String is Hello
Address is 0x80498d0
38
String is Hello
New address is 0x80498d0
(3)使用自动分配内存函数分配内存。 参考程序:
#include
char *newstack;
/*申请一块内存空间*/
newstack=(char*)alloca(a); if(newstack)
/*若成功,则输出空间大小和其起始地址*/
printf(―Alloca (0x%X) returned %p\\n‖,a,newstack); else
/*失败则报告错误*/
printf(―Alloca (0x%X) failed\\n‖,a);
} /*函数退出,内存自动释放,无需干预*/
void main( )
{
/*申请一块256字节大小的内存空间,观察输出情况*/ test(256); /*再申请一块更大的内存空间,观察输出情况*/ test(16384); }
运行结果:
Alloca (0x100) returned 0xbfffd900 Alloca (0x400) returned 0xbfff9a00
实验四 文件管理
3、实验内容 (1)利用用户自定义的缓冲区,使对文件的单字节读/写操作在缓冲区中进行,只有当用户缓冲区空或满时,才调用read/write从(或“向”)文件读、写数据。 #include
#include
39
void wr();
char buffer[SIZ]=”\\0”; int nread=0; int fd;
int main(int argc,char**argv) {
int rw;
if(argc<2){
fprintf(stderr,”illegal operation”); exit(EXIT_FAILURE);}
if(!strcmp(“read”,argv[1])) { rw=1;
printf(“[read mode]\\n”);} else{
if(!strcmp(“write”,argv[1])) { rw=0;
printf(“[write mode]\\n”);} }
switch(rw){ case 0: wr(); break; case 1: rd(); break; default: break;} exit(0); }
void rd() {
if(fd=open(“text”,O_RDONLY))==-1 { fprintf(stderr,”file open error\\n”); exit(EXIT_FAILURE);}
while((nread=read(fd,buffer,SIZ))!=0) { write(1,buffer,nread); } close(fd); }
void wr() {
40