- 1、本文档共29页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
操作系统实验_示例代码_linux
《计算机操作系统》
实验指导
苏州科技学院电子与信息工程系
软件教研室
二OO二年九月
示例代码
第一部分 LINUX 操作系统平台
实验一 命令解释程序
示例程序minishell.c
//文 件 名 minishell.cpp
//功 能 小型SHELL命令解释程序
//开发环境
#define true 1
#define flase 0
#include stdio.h
#include string.h
#include stdlib.h
void main()
{
char cmdl[80];
char *scwt[]={exit,dir,time};
static int cmdnum=3; //可用的命令数
char cmd[80];
int j,n;
while(true)
{
printf(Please input command: );
gets(cmdl); //取命令行输入
n=strcspn(cmdl, ); //取命令命令部分
if (n0||strlen(cmdl)0)
{ strncpy(cmd,cmdl,n);
cmd[n]=\0;
for(j=0;jcmdnum;j++)
if (strcmp(cmd,scwt[j])==0)
break;
if (j==0) //是exit命令?
exit(0);
if (jcmdnum) //其他合法命令
{
system(cmdl);
continue;
}
printf(Bad command!\n); //命令错
}
}
}
实验二 进程管理
fork()调用示例
#include stdio.h
main(){
int p1,p2;
while ((p1=fork())==-1);
if (p1==0) //是子进程?
putchar(b);
else //父进程
{
putchar(a);
}
}
实验三 进程间通信
1)pipe()调用示例
#includestdio.h
main()
{
int id,fd[2];
char buf[50],s[50];
pipe(fd);
while ((id=fork())==-1);
if (id==0)
{
sprintf(buf,Child is sending message!);
write(fd[1],buf,50);
exit(0);
}
else
{
wait(0);
read(fd[0],s,50);
printf(%s\n,s);
exit(0);
}
}
2)共享存储器示例shm_sample.c
#include stdio.h
#include sys/types.h
#include sys/ipc.h
#include sys/shm.h
#define SEGSIZE 100
main(int argc, char *argv[])
{ key_t key;
int shmid, cntr;
char *segptr;
if(argc == 1)
usage(); /* Create unique key via call to ftok() */
key = ftok(., S);
/* Open the shared memory segment - create if necessary */
if((shmid = shmget(key, SEGSIZE, IPC_CREAT|IPC_EXCL|0666)) == -1)
{
printf(Shared memory segment exists - opening as client\n);
/* Segment probably already exists - try as a client */
if((shmid = shmget(key, SEGSIZE, 0)) == -1)
{
perror(shmget);
exit(1);
}
}
else
{
printf(Creating new shared memory segment\n);
}
/* Attach (map) the shared memory segment into the current process */
if((segptr = shmat(shmid, 0, 0)) == -1)
{
perror(s
文档评论(0)