基础知识:
chmod u+x aaa.sh
为脚本文件加上可执行权限
./aaa.sh
运行
1.编写一个shell脚本程序,它带一个命令行参数,这个参数是一个文件。如果这个文件是一个普通文件,则打印文件所有者的名字和最后的修改日期。如果程序带有多个参数,则输出出错信息。
#!/bin/bash
if test $# -gt 1
then
echo "too many parmeters"
exit 1
fi
if test -f "$1"
then
ls -l $1|awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9}'
exit 0
fi
echo "no such file"
exit 1
注:参数看题目要求选
2.编写shell程序,统计指定目录下的普通文件、子目录及可执行文件的数目,目录的路径名字由参数传入
#!/bin/sh
echo "normal files:" `find $1 -type f | wc -l`
echo "subdirectory:"`find $1 -type d | wc -l`
echo "executable files:" `find $1 -type f -executable | wc -l``
3.用bash编写程序,该程序从键盘输入的n个整数,计算其总和、最大值和最小值并输出,从小到大排序并输出。
#!/bin/sh
for i in `seq $1`
do
read var
echo $var>>tempfile.tmp
done
echo "max number is :"`sort -n tempfile.tmp |head -n1 `
echo "min number is :"`sort -rn tempfile.tmp |head -n1 `
echo "sum of all number:"`awk '{ a+=$0}END{ print a}' tempfile.tmp `
rm tempfile.tmp
note:$1是数字的个数 比如3
然后输
1
2
3
就行了
- 本实验目的观察使用带-f选项的tail命令及学习如何使用gcc编译器,并观察进程运行。自己去查阅资“料获取下面源程序中的函数(或系统调用)的作用。。创建一个文件名为test.c的c语言文件,内容如下:
#include <stdio.h>
int main()
{
int i;
i = 0;
sleep(10);
while (i < 5) {
system("date");
sleep(5);
i++;
}
while (1) {
system("date");
sleep(10);
}
return 0;
}
在shell提示符下,依次运行下列三个命令:
gcc –o generate test.c
./generate >> dataFile &
tail –f dataFile
第一个命令生成一个c语言的可执行文件,文件名为generate;
第二个命令是每隔5秒和10秒把date命令的输出追加到dataFile文件中,这个命令为后台执行,注意后台执行的命令尾部加上&字符;
最后一个命令tail –f dataFile,显示dataFile文件的当前内容和新追加的数据:
在输入tail -f 命令1分钟左右后,按终止tail程序。用kill -9 pid命令终止generate后台进程的执行。
最后用tail dataFile命令显示文件追加的内容。给出这些过程的你的会话。
注:pid是执行generate程序的进程号;使用generate >> dataFile &命令后,屏幕打印后台进程作业号和进程号,其中第一个字段方括号内的数字为作业号,第二个数字为进程号;也可以用kill -9 %job终止generate 后台进程,job为作业号。
- 用C语言写一个程序名字为prog,该程序运行过程中共有4个进程,prog程序(父进程)创建2个子进程p1和p2,p1子进程再创建一个子进程p3。4个进程还需要完成如下工作:
1) 父进程打印字符串“I am main process”;
2) 使用任何一种通信机制实现p1进程和p2进程之间的通信,可以使用的通信机制如:管道、消息队列、共享内存等。P1进程发送“Child process p1 is sending a message!”信息给p2进程,P2进程发送“Child process p2 is sending a message!” 信息给p1进程,p1和p2两个子进程接收信息后分别打印这两个字符串。
3) 子进程p3打印字符串“I am child process p3”,然后使用exec(族)系统调用打印当前目录下文件和子目录下的详细信息。
4) 每个进程的要打印自己的pid。
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int pid1,pid2,pid3;
// lockf(1,1,0)是锁定屏幕输出,不让其他进程可以输出到屏幕,lockf(1,0,0)则是解锁
int main(){
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd); /*创建一个管道*/
while ((pid1=fork( ))==-1);
if(pid1==0){
lockf(fd[1],1,0);
sprintf(outpipe,"child 1 process is sending message!");
/*把串放入数组outpipe中*/
write(fd[1],outpipe,50); /*向管道写长为50字节的串*/
sleep(5);
lockf(fd[1],0,0);
printf("PID1 = %d\n",getpid());
exit(0);
}
while((pid2=fork( ))==-1);
if(pid2==0){
lockf(fd[1],1,0);
sprintf(outpipe,"child 2 process is sending message!");
write(fd[1],outpipe,50);
sleep(5);
lockf(fd[1],0,0);
printf("PID2 = %d\n",getpid());
exit(0);
}
else{
wait(0); //在没有唤醒的情况下该线程一直处于等待状态
read(fd[0],inpipe,50); /*从管道中读长为50字节的串*/
printf("%s\n",inpipe);
wait(0);
read(fd[0],inpipe,50);
printf("%s\n",inpipe);
while((pid3=fork())==-1);
if(pid3==0){
printf("I am child process p3\n");
printf("PID3 = %d\n",getpid());
execlp("/bin/ls","ls",NULL);
exit(0);
}
exit(0);
}
exit(0);
}
6.Fibonacci 序列为0 ,1,1,2,3,5 ,8,…,通常,这可表达为:
fib0=0
fibl = 1
fibn = fibn-1 +fibn-2
编写一个多线程程序来生成Fibonacci 序列。程序应该这样工作:用户运行程序时在命令行输入要产生Fibonacci序列的个数,然后程序创建一个新的线程来产生Fibonacci序列,把这个序列放到线程共享的数据中(数组可能是一种最方便的数据结构)。当线程执行完成后,父线程将输出由子线程产生的序列。由于在子线程结束前,父线程不能开始输出Fibonacci 序列,因此父线程必须等待子线程的结束。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h> /*pthread_create()......*/
#include <unistd.h>
pthread_t ntid;
int a[512];
void *thread_fun(void *args){
int k;
a[0] = 0;
a[1] = 1;
for(k=2;k<512;k++)
{
a[k] = a[k-1] + a[k-2];
}
return NULL;
}
int main(void){
int err;
int i,n;
printf("Please enter n:(n>=3 & n<=512) ");
scanf("%d",&n);
/*........*/
err=pthread_create(&ntid,NULL,thread_fun,a);
if (err!=0) {
fprintf(stderr, "......: %s\n", strerror(err));
exit(1);
}
pthread_join(ntid,NULL); /*...1.....*/
for(i = 0 ; i<=n ; i++){
printf("F[%d] = %d\t",i,a[i]);
}
}
g++ ff.cpp -o foo -lpthread
编译
7.在linux系统下的软件开发中,经常要使用make工具,要掌握make的规则。makfie文件中的每一行是描述文件间依赖关系的make规则。本实验是关于makefile内容的,您不需要在计算机上进行编程运行,只要书面回答下面这些问题。
对于下面的makefile:
CC = gcc
OPTIONS = -O3 -o
OBJECTS = main.o stack.o misc.o
SOURCES = main.c stack.c misc.c
HEADERS - main.h stack.h misc.h
power: main.c $(OPJECTS)
$(CC) $(OPTIONS) power $(OBJECTS) -lm
main.o: main.c main.h misc.h
stack.o: stack.c stack.h misc.h
misc.o: misc.c misc.h
回答下列问题
a. 所有宏定义的名字
stack.h misc.h
b. 所有目标文件的名字
main.o stack.o misc.o
c. 每个目标的依赖文件
main.c stack.c misc.c
d. 生成每个目标文件所需执行的命令
gcc stack.c -o stack.o -lm
e. 生成main.o stack.o和misc.o时会执行哪些命令,为什么?
-o 生成目标文件
-lm 链接数据库
9.用C语言写一个名字为myls程序,实现类似Linux的ls命令,其中myls命令必须实现-a、-l、-i等选项的功能。要求myls程序使用系统调用函数编写,不能使用exec系统调用或system()函数等调用ls命令来实现。命令man ls可以得到更多ls选项的含义。
参考:https://blog.csdn.net/qingshui23/article/details/53931555
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
using namespace std;
bool cmp(string a, string b){
return a < b;
}
int main(){
//struct __dirstream typedef struct __dirstream DIR;
DIR *dir;
char str[100];
string res[100];
struct dirent *rent;//struct
dir = opendir(".");
int cnt = 0;
while((rent=readdir(dir))){
strcpy(str, rent->d_name);
if(str[0] == '.')
continue;
if(!str)
continue;
//printf("%s ", str);
res[cnt++] = str;
}
//cout<<"cnt = "<<cnt<<endl;
sort(res, res+cnt, cmp);
for(int i=0; i<cnt; i++)
cout<<res[i]<<" ";
puts("");
closedir(dir);
return 0;
}
搬运代码
https://blog.csdn.net/qq_38310033/article/details/79193321
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
void func1(struct stat * p)
{
switch(p->st_mode & S_IFMT)
{
case S_IFSOCK:printf("s");break;
case S_IFLNK:printf("l");break;
case S_IFBLK:printf("b");break;
case S_IFDIR:printf("d");break;
default:printf("-");
}
}
void func2(struct stat * p)
{
//所有者的权限
if(p->st_mode & S_IRWXU & S_IRUSR)
printf("r");
else
printf("-");
if(p->st_mode & S_IRWXU & S_IWUSR)
printf("w");
else
printf("-");
if(p->st_mode & S_IRWXU & S_IXUSR)
printf("x");
else
printf("-");
//所属组的权限
if(p->st_mode & S_IRWXG & S_IRGRP)
printf("r");
else
printf("-");
if(p->st_mode & S_IRWXG & S_IWGRP)
printf("w");
else
printf("-");
if(p->st_mode & S_IRWXG & S_IXGRP)
printf("x");
else
printf("-");
//其他人的权限
if(p->st_mode & S_IRWXO & S_IROTH)
printf("r");
else
printf("-");
if(p->st_mode & S_IRWXO & S_IWOTH)
printf("w");
else
printf("-");
if(p->st_mode & S_IRWXO & S_IXOTH)
printf("x.");
else
printf("-.");
}
void func3(char * tmp)
{
//Sun Jan 28 15:51:39 2018
int i = 0;
int j = 0;
char sign[64];
for(i = 4, j = 0; i < 16; i++, j++)
{
sign[j] = tmp[i];
}
sign[j] = '\0';
printf(" %s", sign);
}
int main()
{
char * p_time = NULL;
DIR * p_dir = NULL;
long int second = 0;
struct tm t;
struct stat st;
struct dirent *p = NULL;
struct passwd *p_uid = NULL;
struct group *p_gid = NULL;
p_dir = opendir(".");
p = readdir(p_dir);
while(p)
{
lstat(p->d_name, &st);
if(*p->d_name == '.')
{
p = readdir(p_dir);//打开下一个文件
stat(p->d_name, &st);
continue;
}
func1(&st);//查看文件类型
func2(&st);//查看文件权限
printf("%3ld ",st.st_nlink);//连接数
//获取用户名
p_uid = getpwuid(st.st_uid);
printf("%5s", p_uid->pw_name);
//获取组的名字
p_gid = getgrgid(st.st_gid);
printf("%6s", p_gid->gr_name);
printf("%6ld", st.st_size);//查看文件大小
p_time = ctime(&(st.st_mtime));//打印日期
func3(p_time);
printf(" %s", p->d_name);//打印文件名
p = readdir(p_dir);//打开下一个文件
printf("\n");
}
return 0;
}