#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define KU_CPU 337 // define syscall number
int main(int argc, char ** argv) {
int jobTime;
int delayTime;
char name[4];
int wait = 0;
int response = 0;
if (argc < 4) {
printf("\nInsufficient Arguments..\n");
return 1;
}
/* first argument : job time (second)
second argument : delay time before execution (second)
third arument : process name
*/
jobTime = atoi(argv[1]);
delayTime = atoi(argv[2]);
strcpy(name, argv[3]);
// wait for 'delayTime' seconds before execution
sleep(delayTime);
printf("\nProcess %s : I will use CPU by %ds.\n", name, jobTime);
jobTime *= 10; // execute system call in every 0.1 second
// continue requesting the system call as long as the jobTime remains
while(jobTime) {
// if request is rejected, increase wait time
if (!syscall(KU_CPU, name, jobTime)) jobTime--;
else {
wait ++;
response ++;
}
usleep(100000); // delay 0.1 second
}
syscall(KU_CPU, name, 0);
printf("\nProcess %s : Finish! My response time is %ds and My total wait time is %ds. ", name, (response+5)/10, (wait+5)/10);
return 0;
}
/*2024 Fall COSE341 Operating System*/
/*Project 2*/
/*Kim JinHyeong*/
#include <linux/syscalls.h>
#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/sched.h>
#include <linux/slab.h>
# define IDLE -1
typedef struct _job_t {
int pid;
int jobTime;
} job_t;
int now = IDLE;
typedef struct _node{
int pid; // waiting process pid
struct _node * next; // next node
} NODE; // queue elements
typedef struct header {
int num; // number of waiting queue elements
NODE* first; // first element
NODE* last; // last element
} HEADER;
HEADER waiting_header = {0, NULL, NULL};
int ku_pop(void); // dequeue
int check(int x); // check whether x is in waiting_queue
void ku_push(int pid); // enqueue
SYSCALL_DEFINE2(os2024_ku_cpu, char*, name, int, jobTime) {
// store pid of current process as pid_t type
job_t newJob = {current->pid, jobTime};
// register the process if virtual CPU is idle
if (now == IDLE) now = newJob.pid;
// If the process that sent the request is currently using virtual CPU
if (now == newJob.pid) {
// If the job has finished
if (jobTime == 0) {
printk("Process Finished: %s\n", name);
// if queue is empty, virtual CPU becomes idle
if (waiting_header.num == 0) now = IDLE;
// if not, get next process from queue and load
else now = ku_pop();
}
else printk("Working: %s\n", name);
// request accepted
return 0;
}
else {
// if the request is not from currently handling process
if (check(newJob.pid)) ku_push(newJob.pid); // enqueue pid i
f process is not in waiting queue
printk("Working Denied:%s \n", name);
}
//request rejected
return 1;
}
int ku_pop() { // dequeue from waiting queue and return dequeue
d pid valud
NODE* temp = waiting_header.first;
int res = temp->pid;
waiting_header.first = waiting_header.first->next;
waiting_header.num--;
if (waiting_header.num == 0) waiting_header.last == NULL;
kfree(temp);
return res;
}
int check(int new_pid) { // check whether or not the pid of newJo
b is in waiting queue. If it doesn't exist, return 1.
NODE* temp;
int i;
if (waiting_header.num == 0) return 1;
temp = waiting_header.first;
for (i = waiting_header.num; i > 0; i--) {
if (temp->pid == new_pid) return 0;
temp = temp->next;
}
return 1;
}
void ku_push(int pid) { // enequeue the pid of newJob into waiti
ng queue
NODE* new_node = (NODE *)kmalloc(sizeof(NODE), GFP_KERNEL);
new_node->pid = pid;
new_node->next = NULL;
if (waiting_header.num == 0) {
waiting_header.first = new_node;
waiting_header.last = new_node;
waiting_header.num++;
return;
}
waiting_header.last->next = new_node;
waiting_header.last = new_node;
waiting_header.num++;
return;
}



run file
./kucpu_run 7 0 A & ./kucpu_run 5 1 B & ./kucpu_run 3 2 C &
chmod 777 run 명령어를 통해 read, write, execute 권한 부여




