Міністерство освіти України
НУ “Львівська політехніка”
Кафедра ЕОМ
Звіт
про виконання лабораторної роботи №6
з дисципліни
“Проектування операційних систем, утиліт і драйверів”
Назва роботи:
“Файлова система proc”
Мета: Поглибити знання iз файлової системи proc операцiйної системи Linux. Познайомитися iз структурою файла stat, який вiдображає стан процесу. Познайомитися iз системним викликом getpid та структурою task_struct.
Лiстинг структури task_struct iз заголовочного модуля /usr/include/linux/sched.h.
struct task_struct {
/*
* offsets of these are hardcoded elsewhere - touch with care
*/
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
unsigned long flags; /* per process flags, defined below */
int sigpending;
mm_segment_t addr_limit; /* thread address space:
0-0xBFFFFFFF for user-thead
0-0xFFFFFFFF for kernel-thread
*/
struct exec_domain *exec_domain;
volatile long need_resched;
unsigned long ptrace;
int lock_depth; /* Lock depth */
/*
* offset 32 begins here on 32-bit platforms. We keep
* all fields in a single cacheline that are needed for
* the goodness() loop in schedule().
*/
long counter;
long nice;
unsigned long policy;
struct mm_struct *mm;
int processor;
/*
* cpus_runnable is ~0 if the process is not running on any
* CPU. It's (1 << cpu) if it's running on a CPU. This mask
* is updated under the runqueue lock.
*
* To determine whether a process might run on a CPU, this
* mask is AND-ed with cpus_allowed.
*/
unsigned long cpus_runnable, cpus_allowed;
/*
* (only the 'next' pointer fits into the cacheline, but
* that's just fine.)
*/
struct list_head run_list;
unsigned long sleep_time;
struct task_struct *next_task, *prev_task;
struct mm_struct *active_mm;
struct list_head local_pages;
unsigned int allocation_order, nr_local_pages;
/* task state */
struct linux_binfmt *binfmt;
int exit_code, exit_signal;
int pdeath_signal; /* The signal sent when the parent dies */
/* ??? */
unsigned long personality;
int did_exec:1;
pid_t pid;
pid_t pgrp;
pid_t tty_old_pgrp;
pid_t session;
pid_t tgid;
/* boolean value for session group leader */
int leader;
/*
* pointers to (original) parent process, youngest child, younger sibling,
* older sibling, respectively. (p->father can be replaced with
* p->p_pptr->pid)
*/
struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
struct list_head thread_group;
/* PID hash table linkage. */
struct task_struct *pidhash_next;
struct task_struct **pidhash_pprev;
wait_queue_head_t wait_chldexit; /* for wait4() */
struct completion *vfork_done; /* for vfork() */
unsigned long rt_priority;
unsigned long it_real_value, it_prof_value, it_virt_value;
unsigned long it_real_incr, it_prof_incr, it_virt_incr;
struct timer_list real_timer;
struct tms times;
unsigned long start_time;
long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
int swappable:1;
/* process credentials */
uid_t uid,euid,suid,fsuid;
gid_t gid,egid,sgid,fsgid;
int ngroups;
gid_t groups[NGROUPS];
kernel_cap_t cap_effective, cap_inheritable, cap_permitted;
int keep_capabilities:1;
struct user_struct *user;
/* limits */
struct rlimit rlim[RLIM_NLIMITS];
unsigned short used_math;
char comm[16];
/* file system info */
int link_count, total_link_count;
struct tty_struct *tty; /* NULL if no tty */
unsigned int locks; /* How many file locks are being held */
/* ipc stuff */
struct sem_undo *semundo;
struct sem_queue *semsleeping;
/* CPU-specific state of this task */
struct thread_struct thread;
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
/* namespace */
struct namespace *namespace;
/* signal handlers */
spinlock_t sigmask_lock; /* Protects signal and blocked */
struct signal_struct *sig;
sigset_t blocked;
struct sigpending pending;
unsigned long sas_ss_sp;
size_t sas_ss_size;
int (*notifier)(void *priv);
void *notifier_data;
sigset_t *notifier_mask;
/* Thread group tracking */
u32 parent_exec_id;
u32 self_exec_id;
/* Protection of (de-)allocation: mm, files, fs, tty */
spinlock_t alloc_lock;
/* journalling filesystem info */
void *journal_info;
};
Вихідний текст програми#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#define MAXLEN 256
#define PROC_PATH "/proc/"
void get_stat_file_name(int, char *);
int print_file_content(char *, int);
int print_file_header(char *);
int main () {
int pid = getpid();
char fname[MAXLEN];
printf("The pid of the current process is %d\n", pid);
puts("its parents\n");
do {
get_stat_file_name(pid, fname);
} while((pid = print_file_content(fname, pid)) != 1);
exit(0);
}
void get_stat_file_name(int pid, char *fname) {
sprintf(fname, "%s%d%s", PROC_PATH, pid, "/stat");
}
int print_file_content(char *fname, int pid) {
int ppid = print_file_header(fname);
char buf[MAXLEN];
FILE *f = fopen(fname, "r");
while(fgets(buf, MAXLEN, f) != NULL) {
puts(buf);
}
fclose(f);
return ppid;
}
int print_file_header(char *fname) {
int pid, ppid;
char buf[MAXLEN], c;
FILE *f = fopen(fname, "r");
fscanf(f, "%d%s%c%c%c%d", &pid, buf, &c, &c, &c, &ppid);
printf("Process name: %s\n", buf);
printf("Process pid: %d\n", pid);
printf("Process ppid: %d\n",ppid);
fclose(f);
return ppid;
}
Результати запуску програми
Pograms for laborator work #6
Stat-files content demonstration
Author Volodya Shynkliar
**************************************************************
The pid of the current process is 954
Here is the chain of its parents
Process name: (labor6)
Process pid: 954
Process ppid: 784
954 (labor6) R 784 954 784 769 954 0 22 0 95 0 0 0 0 0 14 0 0 0 43065 1224704 93 4294967295 134512640 134514874 3221224528 3221223464 1074621012 0 0 0 0 0 0 0 17 0
Process name: (bash)
Process pid: 784
Process ppid: 779
784 (bash) S 779 784 784 769 954 0 1790 1761 318 876 63 57 23 12 16 0 0 0 33119 2945024 494 4294967295 134512640 135069884 3221224560 3221223844 1074772873 0 65536 3686404 1266761467 3222399628 0 0 17 0
Process name: (mc)
Process pid: 779
Process ppid: 740
779 (mc) S 740 779 740 768 779 0 139 66 310 596 9 54 2 6 9 0 0 0 32933 2711552 343 4294967295 134512640 134970144 3221224624 3221223888 1075075870 0 0 2 134287360 3222579404 0 0 17 0
Process name: (bash)
Process pid: 740
Process ppid: 721
740 (bash) S 721 740 740 768 779 0 1185 1202 318 777 60 81 18 23 9 0 0 0 31332 2945024 493 4294967295 134512640 135069884 3221224608 3221223892 1074772873 0 65536 3686404 1266761467 3222399628 0 0 17 0
Process name: (kdeinit)
Process pid: 721
Process ppid: 470
721 (kdeinit) S 470 470 470 0 -1 64 1341 16 2167 121 177 177 0 7 13 0 0 0 29621 30277632 2288 4294967295 134512640 134543710 3221224832 3221222320 1090153246 0 0 4097 66728 3222579404 0 0 17 0
Process name: (kdeinit)
Process pid: 470
Process ppid: 1
470 (kdeinit) S 1 470 470 0 -1 64 378 6059 247 11217 11 60 1427 1283 8 0 0 0 8684 18874368 898 4294967295 134512640 134543710 3221224832 3221224032 1090153246 0 0 4096 65536 3222579404 0 0 17 0
Висновок
В процесі виконання лабораторної роботи я познайомився з особливостями файлової системи proc ОС Linux. Також було створено програму, яка рекурсивно виводить дані про процес та всіх його предків.
Список літератури.
Кейт Хевіленд, Дайна Грей, Бен Салама “Системное программирование в Unix”. Москва 2000.
Електронні ресурси кафедри ЕОМ.