gpt4 book ai didi

c - C中输出到文件时的随机字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:55 26 4
gpt4 key购买 nike

我正在编写一个先到先得的调度算法。我相信我有正确的算法,并且正在按照我的流程列表的正确顺序和时间处理作业。我唯一的问题是它是如何输出到我的输出文件的。除了列表中的第一个作业外,所有其他作业名称都在正确输出。

这是我的头文件:fcfs.h

#ifndef FUNCTION_H
#define FUNCTION_H

//---------------------------------------------------------------------------
// STRUCTURE THAT HOLDS CPU INFORMATION |
//--------------------------------------------------------------------------

// Struct that simulates the cpu running, cpu1 being the cpu currently running
// tells what the clock pulse is, its current job, and whether or not it is
// occupied
struct cpu
{
int clock_pulse;
struct processes* job;
bool occupied;
}cpu1 = {0, NULL, false};


//---------------------------------------------------------------------------
// FUNCTIONS FOR CPU |
//--------------------------------------------------------------------------

void increment_clock_pulse(); // increments clock_pulse by 1

bool is_cpu_occupied(); // returns true if cpu is occupied, false otherwise

void check_arrivals(); // changes cpu1 state and waiting queue

//---------------------------------------------------------------------------
// STRUCTURE THAT HOLDS INFORMATION FOR PROCESS QUEUE |
//--------------------------------------------------------------------------

// Structure that holds individual nodes for each process in the waiting
// queue. Holds process name, arrival time, service time, priority level
struct processes
{
char name[10];
int arrival_time;
int service_time;
int priority_level;
struct processes *next;
};

struct processes *head = NULL; // instance of processes that points to beginning

struct processes *rear = NULL; // instance of processes that points to end

//---------------------------------------------------------------------------
// BASIC FUNCTIONS FOR LINKED LIST QUEUE |
//--------------------------------------------------------------------------

void enqueue(char *n, int a, int s, int p); // places process at the back of the queue

void dequeue(); // removes the front of the queue

void print_list(); // prints out the list of processes

bool is_empty(); // returns true if empty, false if not

//---------------------------------------------------------------------------
// FUNCTION FOR READING FILE INTO QUEUE |
//--------------------------------------------------------------------------
void fill_array(char *file); // fills in the queue from file input

void output(); // outputs the jobs into output.txt, creates it if it doesn't exist

#endif

这是我的实现文件:fcfs.c

#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcfs.h"

//---------------------------------------------------------------------------
// FUNCTIONS FOR CPU |
//--------------------------------------------------------------------------

// increments the clock pulse by one
void increment_clock_pulse()
{
cpu1.clock_pulse++;
}

// returns true if cpu is occupied, false if not
bool is_cpu_occupied()
{
return cpu1.occupied;
}

// checks the queue if there are jobs ready to be serviced
void check_arrivals()
{
// if job is ready to be serviced and if there is not already a job in the CPU
if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}
}

//---------------------------------------------------------------------------
// BASIC FUNCTIONS FOR LINKED LIST QUEUE |
//--------------------------------------------------------------------------

// Funtcion the takes in a string and 3 integers for its input
// and inserts the data into the queue
void enqueue(char *n, int a, int s, int p)
{
struct processes *temp = (struct processes*)malloc(sizeof(struct processes));
strcpy(temp->name, n);
temp->arrival_time = a;
temp->service_time = s;
temp->priority_level = p;
temp->next = NULL;
if(head == NULL && rear == NULL){
head = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}

// Function that dequeues the first item in the queue and then moves the
// queue forward
void dequeue()
{
struct processes* temp = head;
if(head == NULL) {
printf("Queue is Empty\n");
return;
}
if(head == rear) {
head = rear = NULL;
}
else {
head = head->next;
}
free(temp);
}

// Function that prints out the current queue
void print_list()
{
struct processes *ptr = head;
printf("\n[ ");

while(ptr != NULL){
printf("(%s %d %d %d) ",ptr->name, ptr->arrival_time,
ptr->service_time, ptr->priority_level);
ptr = ptr->next;
}

printf(" ]");
}

// Returns true if the queue is empty, false if it is not
bool is_empty()
{
return head == NULL;
}

//---------------------------------------------------------------------------
// FUNCTION FOR READING FILE INTO QUEUE |
//--------------------------------------------------------------------------

// Function that fills in the queue, takes in an argument that is the
// name of the file that contains the processes
void fill_array(char *file)
{
FILE *fp; // File pointer
fp = fopen(file, "r"); // opens the file to read processes

// checks to see whether or not fopen() is successful
if (fp == NULL)
{
printf("Error while opening file");
exit(1);
}

// reads in data until End of File
int a, s, p;
char n[10];
while(feof(fp)==0)
{
fscanf(fp, "%s %d %d %d", n, &a, &s, &p);

enqueue(n, a, s, p);
}

fclose(fp);
}

void output()
{
FILE *fp;

fp = fopen("output.txt", "a");

fprintf(fp, "%s %d %d \n", cpu1.job->name, (cpu1.clock_pulse - cpu1.job->arrival_time), cpu1.clock_pulse);

fclose(fp);
}

还有我的驱动文件:ma​​in.c

#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "fcfs.c"

int main()
{
char file[20]; // buffer for file name

printf("Please enter your file name: "); // prompts user for file name
scanf("%s", file);

fill_array(file); // fills array from file

// Beginning of the FCFS Algorithm
while(!is_empty())
{
if(cpu1.occupied) // If CPU is busy
{
if(cpu1.job->service_time == 0) // If current job in CPU is done CPU changes to not busy
{
output(); // outputs job to file when job is finished
cpu1.occupied = false;
}
}

check_arrivals(); // checks for arrivals in the waiting queue

if(cpu1.occupied) // If the CPU is occupied job is not done
{
cpu1.job->service_time--; // decrement service time of current job
}

increment_clock_pulse(); // increment the clock pulse
}


return 0;
}

这是我的输入文件:processes.txt

A0 6 15 4 
A1 9 40 6
A2 9 12 9
A3 12 15 4
A4 30 11 2
A5 45 70 1
A6 70 23 9
A7 75 23 5
A8 75 18 7
A9 90 5 6

输出:output.txt

¢ 15 21 
A1 52 61
A2 64 73
A3 76 88
A4 69 99
A5 124 169
A6 122 192
A7 140 215
A8 158 233
A9 148 238

每次运行它时,我都会为写入的第一个进程获得不同的字符串,在本例中为 A0。

我的想法是,我将它们从队列错误地传递到 cpu,这给我带来了困惑的垃圾。

我到处找遍了,找不到任何答案。请帮忙!

最佳答案

这里有一个很大的问题,并且考虑到它很可能相关的症状:

// checks the queue if there are jobs ready to be serviced
void check_arrivals()
{
// if job is ready to be serviced and if there is not already a job in the CPU
if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}
}

您存储 head 的值,然后调用 dequeue() 删除 head 内存。cpu1.job 现在指向未分配的内存 => 未定义的行为

不过,程序“几乎”运行良好,所以这只是内存所有权的问题。我建议更改您的 cpu 数据结构,以便能够按如下方式保存作业的内存(job 上不再有指针):

struct cpu
{
int clock_pulse;
bool occupied;
struct processes job;
}cpu1 = {0, false, {"",0,0,0,NULL} };

然后更改此代码(以及 job-> 现在是 job. 的所有代码)

    if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = *head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}

所以 head 在被释放之前被复制。在这种情况下,您可以确保内存安全。

关于c - C中输出到文件时的随机字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40412472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com