least recently used algorithm in C for operating system - operating-system

I made this code, however this shows a different value for page faults. Please help me.
time is an array which will store the number of times a page is referenced. And "foo" is a function designed to return the minimum time one page in the frame list.
#include <stdio.h>
int p,i,b,a[50],f[30],counter,fault;
int time[10] = {0};
int foo (void);
int main (void)
{
int min,flag,j;
printf("Enter the number of frames\n");
scanf("%d",&b);
for ( i = 0; i < b; i++ )
f[i] = -1;
printf("Enter the number of pages\n");
scanf("%d",&p);
printf("Enter the pages\n");
for ( i = 0; i < p; i++ )
scanf("%d",&a[i]);
fault = 0, counter = 0;
for (i = 0; i < p; i++)
{
flag = 0;
if ( i < b )
{
f[i] = a[i];
fault++;
time[i]++;
}
else
{
for ( j = 0; j < b; j++ )
{
if ( a[i] == f[j] )
{
flag = 1;
time[j]++;
}
}
if ( flag == 0 )
{
min = foo();
printf("The page replaced for %d page is at pos %d\n",i,min);
f[min] = a[i];
fault++;
time[min]++;
}
}
}
printf("The number of page faults are %d\n",fault);
return 0;
}
int foo (void)
{
int z,bar1, bar2;
bar1 = time[0];
bar2 = 0;
for ( z = 1; z < b; z++ )
{
if ( time[z] < bar1 )
{
bar1 = time[z];
bar2 = z;
}
}
return bar2;
}

Related

Problems in second blocks message digest in self-learning SHA-1 algorithm

Output
I am new in learning C programming. Now, I am trying to do SHA-1 for university project. I think this coding by myself. I am trying to do the message digest from the file above 55characters, which means 2 blocks is needed. The first block message digest is correct, but the second block is wrong. I have checked it very many times, but I still not able to find the mistake. Can anyone with experiences able to help me find it out? Thank you.
patients information.txt
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length,number_of_block,str_length;
unsigned int i = 0,j = 0,l = 0,e = 0,n = 0, t=0, k=0,f=0;
float x=0;
char c;
int H[5]={0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0};
unsigned int temp = 0;
FILE *file;
file = fopen("patients information.txt", "r");//Choose the file that want to access
if (file == NULL)//detect the file is empty or not
{
printf("The file is empty");
}
fseek(file, 0, SEEK_END);// move the file pointer to the end of the file
length = ftell(file);//calculate the length of sting in file
fseek(file, 0, SEEK_SET);// move file pointer back to start of file so we can read each character
printf("The length of string is %d\n",length);//check the number of character in string
char *string = malloc(sizeof(char) * (length+1));
while ( (c = fgetc(file)) != EOF)//pass the every character in the file to the string array
{
string[i] = c;
i++;
}
string[i] = '\0';//terminate the string storing
unsigned char long_msg[length+1];
for (i=0;i<length;i++)//pass the pointer array to the unsigned character array
{
long_msg[i]=string[i];
}
printf("The character store in th array is ");
long_msg[length]=128;
for (i=0;i<=length;i++)//check the message in msg array
{
printf("%X ",long_msg[i]);
}
if (length<=55)
{
number_of_block = 1;
}
else if (length>55 && length<120)
{
number_of_block = 2;
}
else
{
x = ((length - 55)/64);//calculate the number of block needed
number_of_block = x+2;
}
printf("\nNumber of block needed is %d\n",number_of_block);
unsigned char blocks[number_of_block][64];
for (i=0;i<number_of_block;i++)//Split the long string into n number of blocks
{
for(j=0;j<64;j++)
{
blocks[i][j]=long_msg[l];
if(l>length)//padding 0
{
blocks[i][j]=0;
}
l++;
}
}
for (i=0;i<number_of_block;i++)//check the blocks content
{
for(j=0;j<64;j++)
{
printf("%X ",blocks[i][j]);
}
}
printf("\nCheck length padding:\n");
str_length = 8*length;//sting length in bits
if (length<32)//if length of string is 1 bytes in hexadecimal
{
blocks[number_of_block-1][63]=str_length;
}
else
{
blocks[number_of_block-1][62]=(str_length>>8);//second last block
blocks[number_of_block-1][63]=((str_length<<8)>>8);//last block
}
for (i=0;i<number_of_block;i++)//check length padding
{
for(j=0;j<64;j++)
{
printf("%02X ",blocks[i][j]);
}
}
unsigned int w[number_of_block][16][4];
unsigned int W[number_of_block][80];
unsigned int A[number_of_block],B[number_of_block],C[number_of_block],D[number_of_block],E[number_of_block];
for (e=0;e<number_of_block;e++)
{
/*The problem is here*/
n=0;
for (i=0;i<16;i++)//split the padding message into w0 to w15 ,exp. w0 = (w[0][1]),....,(w[0][3])
{
for(j=0;j<4;j++)
{
w[e][i][j] = blocks[e][n];
n++;
}
}
for (i=0;i<16;i++)//combine the hex --> 16 block of hexadecimal(W0 to W15)
{
W[e][i] = ((w[e][i][0])<<24 | (w[e][i][1])<<16 | (w[e][i][2])<<8 | (w[e][i][3]));
}
/*Compute message digest*/
A[e] = 0x67452301;
B[e] = 0xEFCDAB89;
C[e] = 0x98BADCFE;
D[e] = 0x10325476;
E[e] = 0xC3D2E1F0;
for (t=0;t<=79;t++)
{
//for t = 16 -> 79
if (t>=16 && t<=79)//prepare W16 to W79
{
W[e][t]= ( (W[e][t-3]) ^ (W[e][t-8]) ^ (W[e][t-14]) ^ (W[e][t-16]) );
W[e][t]= ( ((W[e][t])<<1) | ((W[e][t]) >> (32-1)));//perform circular left shift
}
if (t>=0 && t<=19)
{
f = (B[e]&C[e]) | ((~B[e])&D[e]);
k = 0x5A827999;
}
else if (t>=20 && t<=39)
{
f = (B[e]^C[e]^D[e]);
k = 0x6ED9EBA1;
}
else if (t>=40 && t<=59)
{
f = (B[e]&C[e]) | (B[e]&D[e]) | (C[e]&D[e]);
k = 0x8F1BBCDC;
}
else if(t>=60 && t<=79)
{
f = (B[e]^C[e]^D[e]);
k = 0xCA62C1D6;
}
temp = ((A[e]<<5) | (A[e] >> (32-5))) + f + E[e] + W[e][t] + k;
E[e] = D[e];
D[e] = C[e];
C[e] = ( (B[e]<<30) | (B[e]>> (32-30)));
B[e] = A[e];
A[e] = temp;
}
printf("\n\n");
printf("%08X %08X %08X %08X %08X",A[e],B[e],C[e],D[e],E[e]);//check the value before adding up
H[0] = ( H[0] + A[e]);
H[1] = ( H[1] + B[e]);
H[2] = ( H[2] + C[e]);
H[3] = ( H[3] + D[e]);
H[4] = ( H[4] + E[e]);
}
printf("\n\n");
printf("Message digest:");
for (i=0;i<5;i++)
{
printf("%X ",H[i]);
}
printf("\n\n");
return 0;
}
The wrong output of second block : CE3A1FD0 01464A63 F6766B50 AF97AC62 8D5DBBDD
The output of second block should be: 906FD62C 58C0AAC0 B6A55520 74E9B89D 9AF00B7F

In Flutter and if the number after decimal point is equal 0 convert the number to int

This is a function if the endValueFixed is equal for example 12.0 I want to print the number without zero so I want it to be 12.
void calculateIncrease() {
setState(() {
primerResult = (startingValue * percentage) / 100;
endValue = startingValue + primerResult;
endValueFixe`enter code here`d = roundDouble(endValue, 2);
});
}
This may be an overkill but it works exactly as you wish:
void main() {
// This is your double value
final end = 98.04;
String intPart = "";
String doublePart = "";
int j = 0;
for (int i = 0; i < end.toString().length; i++) {
if (end.toString()[i] != '.') {
intPart += end.toString()[i];
} else {
j = i + 1;
break;
}
}
for (int l = j; l < end.toString().length; l++) {
doublePart += end.toString()[l];
}
if (doublePart[0] == "0" && doublePart[1] != "0") {
print(end);
} else {
print(end.toString());
}
}
You may use this code as a function and send whatever value to end.
if (endValueFixed==12) {
print('${endValueFixed.toInt()}');
}
conditionally cast it to an int and print it then :)

Scheduler in xv6

I am trying to implement mixture of MLFQ and lottery scheduler in xv6. The problem I am encountering is I am making a function which calculates the total number of processes with high priority in a queue and the sum of their tickets. When I print the no_of_tickets within the loop it prints correct value. But when I am printing it before return value. It prints zero and go into forever loop.
Here is my code
int no_of_processes = 0;
int count(int q_rank)
{
struct proc *p; int no_of_tickets = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->queue_rank==q_rank)
{
if(p->state != RUNNABLE)
continue;
no_of_tickets = (no_of_tickets + (p->tickets));
no_of_processes++;
}
}
return no_of_tickets;
}
struct pstat pstat;
void
scheduler(void)
{
struct proc *p;
struct proc *p1;
int f;
for(f=0; f<NPROC; f++)
{
pstat.hticks[f]=0;
pstat.lticks[f]=0;
pstat.inuse[f]=0;
}
for(;;){
int h_index=-1;
int l_index=-1;
sti();
acquire(&ptable.lock);
for(p1 = ptable.proc; p1 < &ptable.proc[NPROC]; p1++){
int no_of_tickets = count(1);
cprintf("%d", no_of_tickets);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->queue_rank == 1)
{
h_index++;
if(p->state != RUNNABLE)
continue;
p->queue_rank=0;
(p->hticks)++;
pstat.inuse[h_index]=1;
pstat.pid[h_index]=p->pid;
pstat.hticks[h_index]=p->hticks;
run(p);
}
}
l_index++;
if((p1->state != RUNNABLE) || (p1->queue_rank != 0))
continue;
(p1->lticks)++;
pstat.lticks[l_index]=p1->lticks;
run(p1);
if(p1->state != ZOMBIE)
{
run(p1);
}
}
release(&ptable.lock);
}
}

How to determine if a string can be manipulated to be rewritten as a palindrome?

I believe this can be achieved by counting the instances for each character in that string. Even if a single character in that string is repeated at least twice, we can declare that string as a palindrome.
For example: bbcccc can be rewritten as bccccb or ccbbcc.
edified can be rewritten as deified.
Some book mentioned we should be using hash table. I think we can just use a list and check for the character count.
Do you think the logic is correct?
Yes, the main idea is to count the times of each char existing in the string. And it will be true if the string has at most one char occurs odd times and all others even times.
For example:
aabbcc => acbbca
aabcc => acbca
aabbb => abbba
No. You don't have to use a hash map (as some of the other answers suggest). But the efficiency of the solution will be determined by the algorithm you use.
Here is a solution that only tracks odd characters. If we get 2 odds, we know it can't be a scrambled palindrome. I use an array to track the odd count. I reuse the array index 0 over and over until I find an odd. Then I use array index 1. If I find 2 odds, return false!
Solution without a hash map in javascript:
function isScrambledPalindrome(input) {
// TODO: Add error handling code.
var a = input.split("").sort();
var char, nextChar = "";
var charCount = [ 0 ];
var charIdx = 0;
for ( var i = 0; i < a.length; ++i) {
char = a[i];
nextChar = a[i + 1] || "";
charCount[charIdx]++;
if (char !== nextChar) {
if (charCount[charIdx] % 2 === 1) {
if (charCount.length > 1) {
// A scrambled palindrome can only have 1 odd char count.
return false;
}
charIdx = 1;
charCount.push(0);
} else if (charCount[charIdx] % 2 === 0) {
charCount[charIdx] = 0;
}
}
}
return true;
}
console.log("abc: " + isScrambledPalindrome("abc")); // false
console.log("aabbcd: " + isScrambledPalindrome("aabbcd")); // false
console.log("aabbb: " + isScrambledPalindrome("aabbb")); // true
console.log("a: " + isScrambledPalindrome("a")); // true
Using a hash map, I found a cool way to only track the odd character counts and still determine the answer.
Fun javascript hash map solution:
function isScrambledPalindrome( input ) {
var chars = {};
input.split("").forEach(function(char) {
if (chars[char]) {
delete chars[char]
} else {
chars[char] = "odd" }
});
return (Object.keys(chars).length <= 1);
}
isScrambledPalindrome("aba"); // true
isScrambledPalindrome("abba"); // true
isScrambledPalindrome("abca"); // false
Any string can be palindrome only if at most one character occur odd no. of times and all other characters must occur even number of times.
The following program can be used to check whether a palindrome can be string or not.
vector<int> vec(256,0); //Vector for all ASCII characters present.
for(int i=0;i<s.length();++i)
{
vec[s[i]-'a']++;
}
int odd_count=0,flag=0;
for(int i=0;i<vec.size();++i)
{
if(vec[i]%2!=0)
odd_count++;
if(odd_count>1)
{
flag=1;
cout<<"Can't be palindrome"<<endl;
break;
}
}
if(flag==0)
cout<<"Yes can be palindrome"<<endl;
My code check if can it is palindrome or can be manipulated to Palindrome
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
//Tested on windows 64 bit arhc by using cygwin64 and GCC
bool isPalindrome (char *text);
int main()
{
char text[100]; // it could be N with defining N
bool isPal,isPosPal = false;
printf("Give me a string to test if it is Anagram of Palindrome\n");
gets(text);
isPal = isPalindrome(text);
isPosPal = isAnagramOfPalindrome(text);
if(isPal == false)
{
printf("Not a palindrome.\n");
}
else
{
printf("Palindrome.\n");
}
if(isPosPal == false)
{
printf("Not Anagram of Palindrome\n");
}
else
{
printf("Anagram of Palindrome\n");
}
return 0;
}
bool isPalindrome (char *text) {
int begin, middle, end, length = 0;
length = getLength(text);
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
return false;
}
end--;
}
if (begin == middle)
return true;
}
int getLength (char *text) {
int length = 0;
while (text[length] != '\0')
length++;
printf("length: %d\n",length);
return length;
}
int isAnagramOfPalindrome (char *text) {
int length = getLength(text);
int i = 0,j=0;
bool arr[26] = {false};
int counter = 0;
//char string[100]="neveroddoreven";
int a;
for (i = 0; i < length; i++)
{
a = text[i];
a = a-97;
if(arr[a])
{
arr[a] = false;
}
else
{
arr[a] = true;
}
}
for(j = 0; j < 27 ; j++)
{
if (arr[a] == true)
{
counter++;
}
}
printf("counter: %d\n",counter);
if(counter > 1)
{
return false;
}
else if(counter == 1)
{
if(length % 2 == 0)
return false;
else
return true;
}
else if(counter == 0)
{
return true;
}
}
as others have posted, the idea is to have each character occur an even number of times for an even length string, and one character an odd number of times for an odd length string.
The reason the books suggest using a hash table is due to execution time. It is an O(1) operation to insert into / retrieve from a hash map. Yes a list can be used but the execution time will be slightly slower as the sorting of the list will be O(N log N) time.
Pseudo code for a list implementation would be:
sortedList = unsortedList.sort;
bool oddCharFound = false;
//if language does not permit nullable char then initialise
//current char to first element, initialise count to 1 and loop from i=1
currentChar = null;
currentCharCount = 0;
for (int i=0; i <= sortedList.Length; i++) //start from first element go one past end of list
{
if(i == sortedList.Length
|| sortedList[i] != currentChar)
{
if(currentCharCount % 2 = 1)
{
//check if breaks rule
if((sortedList.Length % 2 = 1 && oddCharFound)
|| oddCharFound)
{
return false;
}
else
{
oddCharFound = true;
}
}
if(i!= sortedList.Length)
{
currentCharCount = 1;
currentChar = sortedList[i];
}
}
else
{
currentCharCount++;
}
}
return true;
Here is a simple solution using an array; no sort needed
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[256] = { 0 };
unsigned char i[] = {"aaBcBccc"};
unsigned char *p = &i[0];
int c = 0;
int j;
int flag = 0;
while (*p != 0)
{
a[*p]++;
p++;
}
for(j=0; j<256; j++)
{
if(a[j] & 1)
{
c++;
if(c > 1)
{
flag = 1;
break;
}
}
}
if(flag)
printf("Nope\n");
else
printf("yup\n");
return 0;
}
C#:
bool ok = s.GroupBy(c => c).Select(g => g.Count()).Where(c => c == 1).Count() < 2;
This solution, however, does use hashing.
Assuming all input characters are lower case letters.
#include<stdio.h>
int main(){
char *str;
char arr[27];
int j;
int a;
j = 0;
printf("Enter the string : ");
scanf("%s", str);
while (*str != '\0'){
a = *str;
a = a%27;
if(arr[a] == *str){
arr[a]=0;
j--;
}else{
arr[a] = *str;
j++;
}
*str++;
}
if(j==0 || j== -1 || j==1){
printf ("\nThe string can be a palindrome\n");
}
}

How to calculate Easter Sunday in X++?

X++ method to calculate Easter Sunday?
static date dateOfEaster(Yr y)
{
int a = y mod 19;
int b = y div 100;
int c = y mod 100;
int d = b div 4;
int e = b mod 4;
int f = (b+8) div 25;
int g = (b-f+1) div 3;
int h = (19*a+b-d-g+15) mod 30;
int i = c div 4;
int k = c mod 4;
int l = (32+2*e+2*i-h-k) mod 7;
int m = (a+11*h+22*l) div 451;
int n = (h+l-7*m+114) div 31;
int p = (h+l-7*m+114) mod 31;
return mkdate(p+1,n,y);
}
I got a little creative so you can enumerate all of the holidays for a given year using GET with a public web service and a little recursion. Play around with this as you like. Just copy/paste to a JOB:
static void HolidayWebService(Args _args)
{
System.Net.WebClient webClient = new System.Net.WebClient();
str holidaysAvailable = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx/GetHolidaysAvailable?countryCode=UnitedStates";
str holidayDate = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx/GetHolidayDate?countryCode=%1&holidayCode=%2&year=%3";
str retVal = webClient.DownloadString(holidaysAvailable);
XMLDocument doc=XMLDocument::newXml(retVal);
XmlNamedNodemap attributes;
XmlElement root = doc.root();
XmlNode node = root.firstChild();
void getHolidayDate(str _holidayCode, Yr _yr = datetimeutil::year(datetimeutil::utcNow()), str _countryCode = 'UnitedStates')
{
System.Net.WebClient webClientInner = new System.Net.WebClient();
str locRetVal;
;
try
{
locRetVal = webClientInner.DownloadString(strfmt(holidayDate, _countryCode, _holidayCode, _yr));
info(strfmt("[%1] %2", _holidayCode, locRetVal));
}
catch
{
error(strfmt("Error with %1, %2, %3", _holidayCode, _yr, _countryCode));
continue;
}
}
void dig(XmlNode _node, int _depth = 0)
{
XmlNode sib;
;
if (_node == null)
return;
if (_node.hasChildNodes())
dig(_node.firstChild(), (_depth+1));
else
{
if (_node.parentNode().name() == 'CODE')
getHolidayDate(_node.innerText());
}
sib = _node.nextSibling();
if (sib)
dig(sib);
}
;
dig(node);
}